So I am instantiating a missile with this script.
The object is instantiating and moving but it’s moving in a weird direction and NOT towards specified target object.
I thought it might be the transform of the target might be buggy so I had the script get the transform of another object in the scene, but the missile still goes in a weird direction.
How do I get this missile object to travel in the direction of the target? What am I doing wrong?
public class FireMissile : MonoBehaviour {
public Transform target;
public Vector3 dir;
public float speed = 2f;
// Use this for initialization
void Start ()
{
speed = 2f;
target = GameObject.Find("ship").transform;
dir = target.position;
Invoke ("elimin", 3);
}
void Update()
{
dir = transform.position - target.position;
dir.Normalize ();
transform.Translate (dir * speed * Time.deltaTime);
}
void elimin()
{
Destroy (this.gameObject);
}
}