Look At Target?

Hello I am having trouble with my lookAt script I made heres the one that would work

var target : Transform;

function Update () {
    transform.LookAt(target);
}

but the object is going to spawn when my enemy dies so the target wont be set when it spawns here is what I tried for an example of what I am trying to do

var target;

function Update () {
    target = gameObject.Find("Player");
    transform.LookAt(target);
}

can someone please help

Thanks.

2 Answers

2

This one should work:

var target : GameObject;

function Update () {
    target = gameObject.Find("Player");
    transform.LookAt(target.transform);
}


From the docs:

“For performance reasons it is recommended to not use this function every frame Instead cache the result in a member variable at startup or use GameObject.FindWithTag.”

// This is another way of doing this, example from a game I’ve started working on //

//pre defined target
transform.LookAt(target);

if(transform.position.x <= 30){
target = gameObject.Find(“camera_look_2”).transform;
} else {
target = gameObject.Find(“camera_look_1”).transform;
}