Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Update NavMesh at Runtime

Discussion in 'Navigation' started by emrys90, Jun 8, 2017.

  1. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    489
    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?
     
  2. crimi

    crimi

    Joined:
    Jan 23, 2013
    Posts:
    6
    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:

    Code (CSharp):
    1. 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.
     
    Last edited: Jun 9, 2017
  3. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    489
    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.
     
  4. MrKilljoy

    MrKilljoy

    Joined:
    Aug 19, 2015
    Posts:
    8
    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?
     
  5. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    489
    @crimi Sorry, I guess I didn't pay attention to what you replied with. That's exactly what I'm looking for, thanks!
     
  6. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    489
    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?
     
  7. crimi

    crimi

    Joined:
    Jan 23, 2013
    Posts:
    6
    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
     
  8. GameDveloper09

    GameDveloper09

    Joined:
    Dec 24, 2015
    Posts:
    9
    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.
     
    MishaMa likes this.
  9. Tsilliev

    Tsilliev

    Joined:
    Jan 21, 2014
    Posts:
    27
  10. bennett_apps

    bennett_apps

    Joined:
    Oct 9, 2018
    Posts:
    32
    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.
     
  11. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,331
    I haven't had any issues updating the navmesh with the new NavMeshSurface component:

    Full rebuild from scratch (slow):
    Code (csharp):
    1. NavMeshSurface nm = GameObject.FindObjectOfType<NavMeshSurface>();
    2. nm.BuildNavMesh();
    Update only changed tiles (fast):
    Code (csharp):
    1. NavMeshSurface nm = GameObject.FindObjectOfType<NavMeshSurface>();
    2. nm.UpdateNavMesh(nm.navMeshData);
     
    Ony likes this.
  12. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    357
    Is there a reliable way to determine when the BuildNavMesh method has concluded?
     
  13. p02meysamghorbani

    p02meysamghorbani

    Joined:
    Jul 29, 2019
    Posts:
    3