Update NavMesh at Runtime

How do you update a nav mesh at runtime with the new API? I haven't been able to find any decent examples or documentation on it. For example, if I spawn in a new object after baking the nav mesh, how do I update the nav mesh in the area around that object so that it can be walked on/avoided?

https://github.com/Unity-Technologies/NavMeshComponents/tree/master/Assets/Examples/Scripts

Take those files.

LocalNavMeshBuilder.cs && NavMeshSourceTag.cs

LocalNavMeshBuilder = you setup the area
NavMeshSourceTag = add to the floor/plane you want to bake the navmesh


It does seem to work in some cases.

As he stated in the video it does make sense to setup the LocalNavMeshBuilder Area first and at runtime add NavMeshSourceTag to your dynamic generated content.

if you get the error message:

Failed to create agent because it is not close enough to the NavMesh

you can try to tweek the values in your navagent or make sure that you NPC/Player is really on the navmesh. i also experienced that there is a delay if you generate navmeshes at runtime so you could get those messages aswell.

1 Like

That seems to be how to bake a nav mesh at runtime. What I'm trying to do is update a small part of it due to an object being instantiated/destroyed, without having to rebake the entire nav mesh.

This is what I'm trying to do as well, since my project needs to regenerate parts of a destroyable terrain.
Did you find a solution to your issue?

@crimi Sorry, I guess I didn't pay attention to what you replied with. That's exactly what I'm looking for, thanks!

My only other question then is how to do NavMeshSourceTag for colliders instead of only meshes. I looked at the API for NavMeshBuildSource and it seems to only allow you to either manually specify the dimensions of a primitive collider, or to use a mesh. Is there no way for it to instead just use a primitive collider?

I did also figure out that you can change the script execution order.
If you put those scripts LocalNavMeshBuilder.cs && NavMeshSourceTag.cs above the default time in the editor, they will execute first and you dont will have this error "Failed to create agent because it is not close enough to the NavMesh".

https://docs.unity3d.com/Manual/class-ScriptExecution.html

1 Like

i got this error when i try to use these scripts in scene
"SetDestination" can only be called on an active agent that has been placed on a NavMesh.

1 Like

Currently trying to make the local navmesh builder and source tag work, I already posted a question at reddit, sometimes it does work but currently it doesn't work, while the day before yesterday it did, I will keep updating:
https://www.reddit.com/r/Unity3D/comments/9il4ou/help_dynamic_navmesh_at_runtime_partly_working/


The navmesh is used for "static" or unchanging objects. No movement, not being destroyed or added at runtime. If you want an object that changes, add a "navmesh obstacle" component to it, which when used properly allows the object to alter the mesh as it changes/moves/is disabled.

1 Like

I haven't had any issues updating the navmesh with the new NavMeshSurface component:

Full rebuild from scratch (slow):

NavMeshSurface nm = GameObject.FindObjectOfType<NavMeshSurface>();
nm.BuildNavMesh();

Update only changed tiles (fast):

NavMeshSurface nm = GameObject.FindObjectOfType<NavMeshSurface>();
nm.UpdateNavMesh(nm.navMeshData);
15 Likes

Is there a reliable way to determine when the BuildNavMesh method has concluded?

1 Like

you can use this package:

Runtime NavMesh
https://assetstore.unity.com/packages/tools/ai/runtime-navmesh-151397

You really want me to pay 30 for something that should be included in the engine by default?

They focusing on some useless stuff while basic problems are still not solved - this is ridiculous !!!

3 Likes


For anyone wondering, you need to add this to your project to gain access to the additional navmesh API calls: https://github.com/Unity-Technologies/NavMeshComponents

2 Likes

Yeh... What mean changed tiles? Whole surface object as i see? I have flat 500x500units (tiled by 10x10) ground.
And updating takes hundreds of milliseconds, so i have hube lag spike on it.

Any way to make local changes faster?

Hi,

You can look into

nm.UpdateNavMesh(nm.navMeshData);

It is using method

var sources = CollectSources();

That my be your bottleneck. Profile this method and create new, that suite your need (like nm.UpdateNavMeshLight(nm.navMeshData))
As you can see, all you need is changed tiles, and call this method

NavMeshBuilder.UpdateNavMeshDataAsync(data, GetBuildSettings(), sources, sourcesBounds);
1 Like


Collect sources is takes only 0.5ms, 120ms takes WaitForJobGroupID. As i understand it must be async-multithread. But main thread stops, waiting for it.


Changed tiles is the tiles in "Override Tile Size" on NavMeshSurface component. The navmesh has white lines showing the tiles.

For example, I have a 512 and a 2k size terrain with 256 tile size. I use NavMeshObstacle with carving for trees that disappear and it does not hitch the game. I don't know how carving works compared to UpdateNavMesh.

Hi! When changing settings, it takes a long time to generate . How do I solve this problem ?
6507384--733011--upload_2020-11-10_9-55-10.png