x


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.

more ▼

asked Apr 19 '11 at 08:09 AM

Aydan gravatar image

Aydan
180 28 37 47

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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."

more ▼

answered Apr 19 '11 at 08:27 AM

efge gravatar image

efge
5.1k 5 14 38

Thanks so much for replying so quick its just what i wanted

Apr 19 '11 at 08:35 AM Aydan
(comments are locked)
10|3000 characters needed characters left

// 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; }

more ▼

answered Nov 12 '12 at 12:25 PM

nick_taras gravatar image

nick_taras
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2091
x329
x243
x208

asked: Apr 19 '11 at 08:09 AM

Seen: 3259 times

Last Updated: Nov 12 '12 at 12:25 PM