Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to generate NavMesh with different area costs from terrain?

Discussion in 'Navigation' started by Peter77, Oct 20, 2020.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,591
    I have a Unity terrain where I painted several paths. I want these paths to have a different area cost than other parts of the terrain.

    How can I do this?
     
    Last edited: Feb 20, 2023
    chaikadev likes this.
  2. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    646
    If they are painted, you can't - you need Meshes because the NavMesh only cares about Meshes (and Colliders) and not about textures.
    You can create a Mesh along your path (or build it with cubes, they won't be visible in the game and won't collide with anything) and set the Surface Type to something like "easy to walk" instead of the usual "walkable" - then reduce the area cost. Bake. Done.
     
  3. spikeles

    spikeles

    Joined:
    Jan 11, 2019
    Posts:
    9
    Hi, I know this is an old thread, but I came across it whilst looking for this exact issue so I thought it appropriate to add it.

    I couldn't find any solutions that didn't involve creating meshes, which seemed really not great, so I made my own.

    It involves using the NavMeshSurface from the NavMesh building components ( https://docs.unity3d.com/Manual/NavMesh-BuildingComponents.html )along with auto generated NavMeshModifierVolumes

    If you add NavMeshSurface on your terrain, then use NavMeshModifierVolume on objects you place you can modify the cost of an area on the navmesh as you desire at that point. The problem is you have to manually place all your volumes by hand which is a pain, So I wrote a script that samples the terrain texture in a grid across the entire terrain and at each point creates a cube volume for that cost, then rebuilds the mesh, and done...

    Only issue I've come across is that the Off mesh links are no longer generated automatically... A small price to pay...

    GIST for the component is here (it even handles terrain based trees by marking them as nonwalkable):

    ** Hi, this is an edit. Do note, that this GIST here is NOT optimized for large terrains, and Unity will NOT be happy if you attempt this on large terrain. If you must use it on large terrain set step to 10 or 100 or comment out line 139. Line 139 will delete the nav hint boxes, if you leave them then you can edit the nav hint boxes to fit better, then use Bake in the NavMeshSurface. **

    https://gist.github.com/druggedhippo/76febb3e1a7375e49ff0180cebcbc0eb

    Add the component on your terrain, adjust the layer if needed, add the areaIDs (eg, Walkable, Path, Not Walkable) that match the textures on your terrain (eg, Grass texture, path texture, slime texture) add a NavMeshSurface and trigger the GenMeshes function ( my version uses Odin for the button but feel free to remove that and make your own inspector or whatever that regenerates onValidate ). Note that you should NOT use BAKE button in either the global navmesh OR the NavMeshSurface component.

    Image example:
     
    Last edited: Jan 20, 2022
  4. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    708
    Thank You this was very helpful..
     
  5. kietgula

    kietgula

    Joined:
    Feb 23, 2022
    Posts:
    4
    Hello, I know this is an old thread. But I got a problem when trying to make a specific area to have lower cost using NavMeshModifierVolume.

    The problem is that area will absolutely separate from others. And I can't navigate/caculatepath into that area anymore.

    upload_2024-1-24_10-35-3.png

    I also found that this is not a case in build-in navigate system unity. This is only happend when I use navigate component to bake navmesh.

    Can someone help me please