- Home /
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.
Answer by efge · Apr 19, 2011 at 08:27 AM
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."
Thanks so much for replying so quick its just what i wanted
Answer by nick_taras · Nov 12, 2012 at 12:25 PM
// 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; }
Your answer
Follow this Question
Related Questions
Find Transform in the scene 2 Answers
FindClosestEnemy() change target? 1 Answer
Using GameObject.Find 1 Answer
GameObject.Find closest ?? 2 Answers
Creating a single-line function for GameObject.Find and GetComponent (for multiple components) 3 Answers