transform.lookAt More info then needed?

if(Mathf.Abs(angle)<deadZone) // we do not want the enemy to turn when the difference between their foward direction and desired velocity is very small
{
transform.LookAt (transform.position + nav.desiredVelocity); //looking at desiredVelocity from its own position
angle = 0f; //set the angle to zero so it has no effect from mechanim
}
}

I was double checking a script to see if I understood everything and one thing that bothers me is this line

transform.LookAt (transform.position + nav.desiredVelocity);

Is there a reason for adding transform.position to the navMesh’s desired velocity? Couldn’t you just set the code like this transform.LookAt (nav.desiredVelocity); since you’re already looking at it from the transform’s position anyway?

It’s important to distinguish between global and local space:

  • Local space is relative to a particular transform (“five feet in front of me”).
  • Global space is relative to the world (“five feet to the right of the origin”).

You’re thinking in terms of local space, but the LookAt function expects a global coordinate to look at.

Suppose your player is at position (500, 200, 300) with velocity (1, 0, 0).

Compare the difference in results:

//player looks at (501, 200, 300), which is right next to them
transform.LookAt(transform.position + velocity);

//player looks at (1, 0, 0), which is nonsense as far as the player can tell
transform.LookAt(velocity);

The TransformPoint function is particularly useful for situations where you’d like to take your position and rotation into account:

//five units in front of me, two units above me
var offset = Vector3(0, 2, 5);
var position = transform.TransformPoint(offset);

No, transform.LookAt expects a target specified in world coordinates, so it needs to be the curren location + the desired velocity. Unity - Scripting API: Transform.LookAt