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

[SOLVED] Problem with Unity NavMesh and multiple agent sizes (with a workaround solution)

Discussion in 'Editor & General Support' started by m-i-e-c-z, Apr 16, 2013.

  1. m-i-e-c-z

    m-i-e-c-z

    Joined:
    Nov 10, 2010
    Posts:
    27
    Problem

    Hi guys,

    I have a problem with Unity NavMesh and several agent sizes. My game uses both small human characters and large monsters. Monsters have three times bigger radius compared to humans. When baking NavMesh you can only set one global Radius (the distance from walls and static obstacles in which NavMesh will be generated). So you end up with either large characters intersecting with obstacles or small characters stopping too far away from walls.

    You can set a radius in each individual NavMeshAgent but that is not used for path finding, just avoiding other agents. Could you suggest a smart solution for this problem?

    Workaround solution
    I have found one working workaround solution. Here is what you can do:

    1. Set the global baking radius for your small characters. In my case the default 0.2 units was OK. I assume your smallest characters can walk on Default NavMeshLayer.
    2. Create a new"LargeCharacters" NavMeshLayer for your large characters. To do so go to Edit->Project Settings-> NavMeshLayers.
    3. Your main walkable geometry should have the Default NavMeshLayer.
    4. Add a plane to your scene.
    5. Assign "LargeCharacters" NavMeshLayer to your plane.
    6. Copy your plane, scale it, rotate and move to create custom walkable areas for your large characters. Try to keep your planes as close to the original level collision as possible.
    7. Bake your NavMesh.
    8. Set your small characters' NavMesh Walkable mask to Default and LargeCharacters. Set you large characters' NavMesh Walkable mask to LargeCharacters only. This way your small characters will be able to walk on both NavMeshLayers and your large characters will be restricted to LargeCharacters NavMesh Layer only.
    9. After you finish working with NavMesh baking you can destroy your temporary planes. NavMesh data will not be destroyed. Here I have a small TIP. Parent all your temporary planes to an empty object placed in 0.0.0 world coordinates. You will be able to create a prefab from this object. Update the prefab after each change. So you can delete all the temporary planes and bring them back if you want to change something by simply placing the prefab on the scene in 0.0.0 coordinates.

    Here are two small screenshots from the modified Mecanim NavMesh Integration demo.
     

    Attached Files:

    Last edited: Apr 16, 2013
    Akathon and hanbekov like this.
  2. GerryM

    GerryM

    Joined:
    May 1, 2012
    Posts:
    20
    Nice, thanks for the tutorial.

    Might be burdensome with the manual planes for complex levels, so, still hoping for a more advance NavMesh in Unity one day.
     
  3. m-i-e-c-z

    m-i-e-c-z

    Joined:
    Nov 10, 2010
    Posts:
    27
    If you're using an external 3D package, you can optimize the pipeline for creating this custom NavMesh for large characters. To do this:

    1. Create a level block-out in your 3D app.
    2. Create a mesh that will represent the NavMesh for your large opponents. You can use boolean operations and so on. This mesh doesn't have to be "clean".
    3. Import your block-out to Unity as a prefab.
    4. Set your NavMesh object (created in 3D app) as walkable static and assign an appropiate NavMeshLayer to it.
    5. Decorate your level and delete temporary meshes on the way.
    6. Bake NavMesh.

    Here are some screenshots.

    $NavMeshBlockout_baked.png $NavMeshBlockout.png
     
    tonimarquez84, bitreal and IntegraC like this.
  4. m-i-e-c-z

    m-i-e-c-z

    Joined:
    Nov 10, 2010
    Posts:
    27
  5. pneill

    pneill

    Joined:
    Jan 21, 2007
    Posts:
    207
    Thanks for sharing the workaround. I've posted this question several times myself.

    Also I up voted the feature request. :)
     
  6. m-i-e-c-z

    m-i-e-c-z

    Joined:
    Nov 10, 2010
    Posts:
    27
    Great. I hope the workaround helps a bit. I am currently working on a game that will use multiple agent sizes, so I will surely develop a working solution to that problem. My results will be posted here (that will take some time though).
     
  7. m-i-e-c-z

    m-i-e-c-z

    Joined:
    Nov 10, 2010
    Posts:
    27
    I've played around a bit more with this and my workaround can be only applied for small levels crafted in external 3D packages. If you want to use Unity Terrain or simply create your scenes inside Unity with prefabs... that solution will make you mad quite quickly. It basically means modelling your navigation mesh by hand, every time you change something.

    My attempts with sweep tests and reevaluating the paths also failed. I've tried to find a path, test it with a sweep test and try to find another path if the sweep test hits any obstacles. But it practically means coding your own pathfinding solution from scratch.

    I think that we can only wish for a more complete pathfinding solution built inside Unity. I'm far from complaining, but current one lacks this, I think basic feature of taking the size of multiple agents into consideration when creating the NavMesh.

    We can calculate the NavMesh for a certain agent radius already. We can set multiple agent sizes already. We even have those NavMesh layers with costs and we can assign agents to specific layers. But we can't automatically generate NavMesh with layers for specific agent sizes. So it basically makes it impossible to use small and large agents at the same time.

    In my opinion it would be enough to generate NavMesh in several passes, one for specified agent size. So instead of putting just one agent size, we should maybe have an array of them in the NavMesh baking tool?

    NavMesh system in Unity has a lot of cool features, but this one really seems like a basic functionality.

    If you find this thread please vote for this feature (or maybe it is a fix?): http://feedback.unity3d.com/suggestions/using-navmeshagents-sizes-for-pa
     
  8. m-i-e-c-z

    m-i-e-c-z

    Joined:
    Nov 10, 2010
    Posts:
    27
    OK, since Unity 5.6 this is no longer a problem - now you can bake multiple navmeshes as they are component based. Here you can find a talk about this:
    .
     
    NoSkillz, tiagokb, Luthfan123 and 5 others like this.
  9. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    wow! Thanks!!