- Home /
When is the NavMesh updated after a carving (obstacle) change
I have agents navigating using Unity's built-in NavMesh (Unity 5.6). I also have moving obstacles, which carve holes in the mesh, but only when they are stationary.
I want my agent's to re-path, after a new hole is carved into, or an existing hole removed, from the NavMesh. But if I attempt to trigger re-pathing via the obstacle gameobject broadcasting a message once it is stationary (Time to Stationary = 0), often the agents are re-pathing before the NavMesh has actually been updated.
So when does the NavMesh update, and how can I tell?
For example, is it always updated on the next FixedUpdate cycle? Always by the end of that frame?
Is there a way to tell if the NavMesh has un/carving changes pending, and delay path-finding until it doesn't?
Can my agents subsribe to an event to stay updated about NavMesh changes?
Any insight into the NavMesh update / recalculation process would be much appreciated!
Answer by SuperScience · Nov 04, 2019 at 02:57 AM
Hi Silverlode,
I ran into the same problem that you did. I have a solution, even if its kinda hacky. If someone else has a better solution, please let me know. However, the solution that I am about to describe does work .
Set all non-moving characters to NavMeshAgent.enabled = false and NavMeshObstacle.enabled = true. This doesn't seem to glitch.
Set moving characters to NavMeshObstacle.enabled = false and keep NavMeshAgent.enabled = false. Kick off a coroutine. Have the coroutine check the NavMesh SamplePostion once per frame (or however often you want to do it).
NavMeshHit hit;
if (NavMesh.SamplePosition(turn.transform.position, out hit, 1.0f, NavMesh.AllAreas)){ /*stuff */ }
If the character is ON MESH (this is important) and the Obstacle is still carving the NavMesh, the hit.distance will be > 0 . The number returned should actually be the (diameter of your obstacle + your avoidance threshold).
If the character is ON MESH and the Obstacle is no-longer carving the NavMesh, the hit.distance will become ZERO. You can then turn your NavMeshAgent back on safely.
Hope this helps!