- Home /
How to get a prefab to look at a target?
I'm trying to get an enemy to look at a target, but I can't get it to work. I tried using this too: var target : Transform;
function Update () { transform.LookAt(target); }
But I can't set the value of "target" because the enemy is a prefab! Is there anyway to set the value of target in the script? I tried using this: var target : Transform;
function Update () {
target = GameObject.Find("I put the name of my target here");
transform.LookAt(target); }
But I can't use that because it can't convert a GameObject to a transform. I also tried using "transform.Find" but that didn't work either. Thanks in advance! (P.S. I'm using unity 3.5)
Answer by Doddler · Jan 30, 2012 at 11:39 PM
I think what you're looking for is transform.LookAt(target.transform), unless it's done differently in JS than c#.
Transform is a property of GameObject, which you're actually already invoking for the local object when you call gameobject.transform.LookAt (although you can omit gameobject when it's called on a script that is attached to a gameobject).
Your answer
Follow this Question
Related Questions
How to make a GameObject separate from a prefab? 0 Answers
Spawn Shield to all objects 2 Answers
Prefab Instantiate children transforming to wrong position 1 Answer
Tower defense target update 0 Answers
Please manage the error 'Can not instantiate objects with a parent which is persistent.' 1 Answer