- Home /
How do I get GameObjects to look at me?
var player : GameObject;
var speed : float = 1;
function Start ()
{
player = GameObject.FindGameObjectWithTag("Player");
if (!player)
Debug.Log ("ERROR could not find Player!");
}
function Update()
{
if (!player)
return;
var distance = Vector3.Distance( player.transform.position, transform.position);
if ( distance < 100 )
{
Debug.Log ("player is close");
var delta = player.transform.position - transform.position;
delta.Normalize();
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
}
else
{
Debug.Log("not close yet " + distance);
}
}
I'm using this as my movement script, and I've made a GameObject using blender and put it in a prefab. The problem is that when I use the LookAt() function (even if I put it in a separate script), the enemy I've created gives me its side rather than its front, and it always stays a large distance away from my Controller. Is there any function I can use, or any code block that would make the enemy turn that extra 90 degrees to look at my character? Even better is there some way I can fix this from the inspector itself? I've tried rotating the prefab but that doesn't work either.
I got the code from another question on this website but for the life of me I can't remember the URL of that question.
Answer by flaviusxvii · Dec 04, 2013 at 03:13 PM
Transform.LookAt will make the forward vector of a gameobject point anywhere you like. If you find that your object doesn't look like it's facing the right direction, it may be that your mesh itself is rotated 90 relative to the unity "forward".
Answer by Grohowiak · Dec 04, 2013 at 05:47 PM
I'm not sure this will help but in the tutorial below you can see how to follow a object using Tags and transform.LookAt(follow);
Hope it helps.