1

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?

AdamTheGun
  • 61
  • 1
  • 1
  • 3

4 Answers4

2

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]);  
}
Nickamus
  • 29
  • 3
1
if(agent.remainingDistance < agent.stoppingDistance) 
{
    agent.updateRotation = false;
    //insert your rotation code here
}
else {
    agent.updateRotation = true;
}

This will rotate your agent when it's distance below the stoppingDistance variable. However it will look inhuman, so if you're going for a humanoid ai I'd recommend looking at the unity Mecanim demo (particularly the locomotion scene) as it has code and animations that will properly animate the agent.

Django
  • 28
  • 1
  • 4
0

Maybe try this Head Look Controller. It's very smooth and blends with animations!

HoloLady
  • 1,041
  • 1
  • 12
  • 28
  • This does look nice but its not what I was looking for. Basically I need to find out how to rotate the whole navmeshagent body to face my character while its below the stopping distance. But thanks for the reference to this. I will definitely want to implement this. – AdamTheGun Mar 08 '16 at 08:47
  • Well, in that case maybe try combining the `LookAt()` with a [Lerp](http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html). Make sure to put it in an updating loop though. – HoloLady Mar 08 '16 at 08:51
  • I can do that however it doesn't invoke my animator to do a stationary turn . it won't look natural. That's why I was wondering if there was a way to do it using the navmeshagent as it will invoke the idle turn animation. – AdamTheGun Mar 08 '16 at 09:02
  • I know that this is an old topic but for completion's sake - You would put in the lerp'd LookAt code myAnimator.SetFloat ("myStationaryTurningFloat", turnValue) line so that it updates the animator to do the correct animations. – CherryCoke May 13 '18 at 15:26
0

Put the char in a game object and copy the navmesh from the char to the parent, uncheck enable in char. move any scripts up too. Just spent 5 hours to find this.

Paul Moore
  • 136
  • 1
  • 16