Unity – Nav Agent Stationary Turning

      Comments Off on Unity – Nav Agent Stationary Turning

Unity3d Nav Agent objects provide a great convenience in regards to pathing and obstacle avoidance in AI controlled characters and objects. By default when you control the nav agent you do so by giving it a destination Vector and by interacting with the nav mesh that’s baked into the scene it will determine the best route that avoids running into obstacles.

At runtime the object moves and turns towards the destination based on the values provided to the nav agent for movement and turning speed. Moving in different ways than just facing a direction and going forward, such as strafing or looking around in place, is slightly more complicated. The nav agent doesn’t provide independent control over the direction it’s facing, it assumes that the direction It’s moving is also the direction it should face. You can control whether or not the agent turns or moves through the scene via the public variables updatePosition and updateRotation. By toggling these you can gain some control over movement and turning, but it all still works off the destination. By turning off movement and setting the destination to some relative vector, such as current position + right will have the nav agent turn towards the right, but not move as well. Inversely you could lock the rotation update and continue moving to simulate a strafing maneuver.

LookInPlace

In the above image the cylinder represents the nav agent location which is focusing the direction of the character. When it comes time to restore control to the nav agent you need to ensure that the agent and the object it’s controlling are at the same starting position. If you restore movement functionality to the nav agent without first re-aligning the positions the character will warp to wherever the agent is. This can be achieved with the Warp function, which transports the nav agent target directly to whatever position you give it.

FullControl