Unity Navmeshagent won't face the target object when reaches stopping distance

AdamTheGun picture AdamTheGun · Mar 8, 2016 · Viewed 9.7k times · Source

I'm trying to get the NPC to look at the main character when I'm talking to him. I need to make sure it looks natural and that he is facing me. I know I can do Transform.LookAt() but that is too instant and unnatural.

How do I rotate the navmeshagent to face my character when its stopped moving?

Answer

Nickamus picture Nickamus · Sep 9, 2017

Try this out to control the body orientation - the slerp is adjustable to your desired rotation speed (https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html):

private void FaceTarget(Vector3 destination)
{
    Vector3 lookPos = destination - transform.position;
    lookPos.y = 0;
    Quaternion rotation = Quaternion.LookRotation(lookPos);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, [fill in desired rotation speed]);  
}