- Home /
Would someone tell me why this doesn't work?
I'm trying to wrap my head around Arrays, and I'm taking it one bite at a time, what I don't understand is why Transform.LookAt doesn't work with this, it seams like I'm grabbing one enemy in the array and telling the object to look at it, but that must not be correct.
function Update () { var Enemies : GameObject[]; Enemies = gameObject.FindGameObjectsWithTag("enemy");
var close = (Vector3.Distance(transform.position, Enemies[1].transform.position)> 1);
if(close){ transform.LookAt(Enemies[0]); }
}
So why doesn't this work?
The console should be the one telling you why it doesn't work. Did the error message(s) not make sense to you?
Answer by Borgo · Jan 31, 2011 at 08:03 PM
(Maybe) Another problem
in the docs: function LookAt (target : Transform, worldUp : Vector3 = Vector3.up) : void
It needs to get a transform and your array is for GameObjects.
I can't try this now, but, try this too:
transform.LookAt(Enemies[0].transform);
Can you post the console error too?
That was it! thanks, I gotta remember to focus on the basics.
Answer by DaveA · Jan 31, 2011 at 06:14 PM
Enemies[0] would be the first, if only, such object found.
This line:
var close = (Vector3.Distance(transform.position, Enemies[1].transform.position)> 1);
results in a boolean (if the distance is > 1) So transform.LookAt(close) will fail, because it can't look at a boolean. Perhaps you want
if (close)
transform.LookAt(Enemies[0].transform);
??