- Home /
How to use Unity's pathfinding system without NavMeshAgent controlling my character?
I am making a zombie game and i want the zombies to use Unity's built-in pathfinding system, but i want to control the zombies myself (with a NON-kinematic rigidbody and using AddForce to move them) and have the pathfinding system just tell me where the zombie should go.
I have seen the manual, tutorials and script reference, and i was impressed to find that they all seem to take for granted that the NavMeshAgent should control the character, as i can think of various reasons why someone would not want that (such as in my case), and it seems to me that there is obviously no technical reason why this should be the case. I don't mind losing some non-core features of the system, such as off-mesh links, moving obstacle avoidance, etc.
The only thing that i could think of was to set NavMeshAgent.updatePosition to false so that it can't control the zombie's Transform, then constantly set NavMeshAgent.nextPosition to match the transform's position so that the NavMeshAgent doesn't simply continue moving in its internal simulation, then reading NavMeshAgent.path to find out the direction in which to move the zombie. But i don't know if this will work or if it will cause bugs or intense performance costs (wouldn't constantly setting NavMeshAgent.nextPosition make it have to constantly recalculate a path?).
If there is no way to achieve what i want using Unity's built-in system, then i would appreciate a recommendation on a free third-party pathfinding system that can do what i want (if there are any).
If it matters, my game is 2d top-down (not isometric, but real top-down).
Answer by FortisVenaliter · Jul 09, 2017 at 03:16 PM
Yes, just use the NavMesh.CalculatePath() function and nothing else. You don't even need a NavMeshAgent.
There is one big caveat to this, however. Since you're controlling movement, you have to ensure manually that the start position is properly on the NavMesh before calling CalculatePath, and you have to handle any mid-path recalculations or obstacle avoidance.
Thanks! I will try this in 10-14 hours (i'm going to bed now because of messed up sleep). I will mark the answer as correct because if i run into any problems i can't solve it will probably be something for another question.