Homing Missiles

How can you do homing missiles like in this video?

I've tried the code from the SmoothLookAt script, but it sends the missiles in the up or down direction when I would like them to be constrained to one axis like in the video. Thanks in advance.

well you have to use a LookAt function like this :

var target : Transform;
var speed : float = 5;
function Update () {
     transform.lookAt(target);
     transform.Translate(Vector3.forward*speed*Time.deltaTime);
}

this will make the missile follow the target you set… but then you need to set the target at runtime(because you cant set the target var in the inspector every time you shoot :wink: )

so the way i would do this would be to

1# find a tag in the missile and track that tag

or

2# use a rayCast to find a target

i would recommend using first one…

so your code should look
like this:

var targetTag : String;
var target : Transform;
var speed : float = 5;

function Start () {
    var tar = GameObject.FindWithTag(targetTag);
    if(tar)
    target = tar;
}    

function Update () {
     transform.lookAt(target);
     transform.Translate(Vector3.forward*speed*Time.deltaTime);
}

if you need the other kind of tracking (#2) then just comment :wink: