Search Unity

Question Navmesh Agents floating above ground

Discussion in 'Navigation' started by ryxwaer, Jan 22, 2023.

  1. ryxwaer

    ryxwaer

    Joined:
    Feb 14, 2021
    Posts:
    1
    Hello everyone,

    I am messing around with the native Unity navmesh + navmesh components to be able to bake it in runtime. I already figured out how to set navmesh surface and bake the navmesh from script after the level is procedurally generated. But I have an issue where navmesh agents are floating above ground.


    Is there a way to set agents to navigate on navmesh but walk on the actual colider of ground mesh? From what I searched an option to bake height mesh would solve this but this option isn't available for baking in runtime.
    Also setting offset for navmesh agents won't work as there are different areas where offset between ground and navmesh is from 0.1 to 0.2.
    I saw that navmesh is reworked in Unity version 2022. Should I go for beta to work with better version of navmesh?

    To be more specific:
    I have a empty game object "ChunkGen" that generates all chunks of small island as its children. That root object has a NavMeshSurface component attached and after all the chunks are generated the function BakeNavMesh() is called to bake everything into single navmesh.

    I am trying to make it work for weeks already and can not figure it out.
    So if anyone could point me in the right direction I would be very glad for it.

    Thanks,
    Adam
     
  2. androvisuals_unity

    androvisuals_unity

    Joined:
    Mar 23, 2020
    Posts:
    47
    That's very unusual. FIrst of all try it with one unit.
    Do you get this result with a flat plane or cube?
    Is your navagent a child of a parent transform? If so then this could result in the floating.
    Try building a new navagent with just capsule meshes and colliders, if they don't have a problem then it's in your setup of the model and all other components working together
     
  3. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    278
    I just use a cheap cheat.

    • Make sure the NavMeshAgent has it's enemy or NPC as a child object.
    • Get the exact navmesh position, the value you'd normally pass to SetDestination.
    • Do a raycast down from a little higher, say a 0.5 units or so. Depends on your setup.
    • Set the enemy or NPC to this more exact height.
    • As a bonus, implement foot IK.
    This does impact performance as you'll do a raycast every frame.