How to manually rotate a NavMeshAgent before pathfinding

Hi,

I have two coroutines running in sequence, the first to rotate a NavMeshAgent NPC to face a position and the second to then make it move there.
For the rotation, I overwrite Transform#forward on the transform of the NPC GameObject and for the movement I set NavMeshAgent#SetDestination.

Both steps work, but when the pathfinding starts the initial steering direction seems to be the “old” forward direction from before the rotation instead of the current one, meaning the path is curved where I would expect a straight line (no obstacles in the vicinity) and the debug visualization of the steering direction (cyan arrow when the NavMeshAgent is selected) points almost along the initial un-rotated forward direction instead of the current one in the first frame of navigation.

Are manual changes to the transform values not respected by the NavMeshAgent, do I need to disable NavMeshAgent#updateRotation and handle all rotation myself to make this work? Or is there some other way to rotate a NavMeshAgent?

Try disabling the NavMeshAgent while turning him, then re-enabling him.

I think this gate will “retake” the rotation.

Remember the agent is in charge of position and rotation too, much the same way Rigidbodies and CharacterControllers take over your GameObject’s transform.

Thanks, tried disabling the NavMeshAgent at the beginning of the rotation coroutine, re-enabling it at the end, but still get the same curve along the initial direction sadly.

Oh boy, I forgot checking the NavMesh itself, I had the NPC layer accidently included in the bake so it created a hole in the NavMesh around it. That’s where the curve that fit the original direction purely by coincidence came from.
:man_facepalming:
So it seems manual updates to the transform are respected by NavMeshAgents.

1 Like