Constraining NavMeshAgent to a couple axis? IE : Forward,back,left,right only

I’d like to make a NavMeshAgent that only travels in constrained axis - Forward,back,left,right only. My initial thought is I could check NavMeshAgent.DesiredVelocity every update and snap the vector to one of the four directions based on which angle check is smallest (checked between desired vector and four cardinal directions). Then either set the NavMeshAgent.Move or .velocity to that vector.

  • I think I could only allow a direction change every x random amount of time or it could do an ugly jaggedy switching between directions when desiredVelocity is a 45* vector.

  • Once desired vector matches a cardinal direction, make sure to force Move / or velocity to that vector in order to reach the destination.

Does this sound about right? Is there a better way to do this?

Thanks!

Well this worked nicely using NavMeshAgent.velocity and the method I described. One thing to note is you can’t modify the agent velocity unless it has a destination and is moving already for some reason. You don’t have to go to that destination but if the agent ever fully stops, something seems to get turned off and you can’t modify velocity till agent moves on its own again.