- Home /
Question by
Crispinator · Oct 17, 2017 at 02:07 AM ·
followtargetingmissilehoming
Homing Missiles that target where you were
I have a code that is on a prefab of a missile that is created by a separate object. Its position is then controlled by this code. I'd like the missile to lock onto the player's position when the missile is created and then go to where the player was, NOT where he is now. That is why I have the code that targets the player in an area separate from void Update(). Any help is much appreciated.
void Awake () {
myTransform = transform;
}
void Start () {
MissileTravel();
}
// Update is called once per frame
void Update () {
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
void OnCollisionEnter (Collision collision) {
Destroy(gameObject);
}
void MissileTravel () {
target = GameObject.FindWithTag("Player").transform;
transform.position = new Vector3(Random.Range (-9, 8), 12, 0);
Missile();
transform.Rotate (90,90,90);
Destroy (gameObject, 5);
}
void Missile () {
Debug.Log ("Poop");
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
}
Comment
Try moving the target = GameObject.FindWithTag("Player").transform;
into the Awake ()
method ins$$anonymous$$d.
Your answer
![](https://koobas.hobune.stream/wayback/20220612141217im_/https://answers.unity.com/themes/thub/images/avi.jpg)