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);
}
}
↧