Search Unity

Get Current NavMesh Triangle

Discussion in 'Navigation' started by Andrew-Carvalho, Sep 8, 2020.

  1. Andrew-Carvalho

    Andrew-Carvalho

    Joined:
    Apr 22, 2013
    Posts:
    45
    I was wondering if there was a built in way to get the navmesh triangle that the agent is on at runtime. I know that in editor, the current triangle is shaded, so Unity is internally calculating this somewhere. I was hoping to reuse that logic or call if it was cheap (as opposed to getting the full triangulation data and solving for which triangle the agent is in).
     
  2. Skynet766

    Skynet766

    Joined:
    Sep 13, 2020
    Posts:
    22
    Unless the API changed very recently, Unity does not give you the method to do this. I've always had to do it myself. I think the Unity team is trying to keep the API as simple as possible and avoid details like "triangles". But they still give the triangles, in case you absolutely have to do more advanced things.

    The real headache is performance. If you have a lot of triangles, you need to do your "is the agent on this triangle" check with as few triangles as possible.

    I used a Octree, specifically this one I wrote https://www.nuget.org/packages/OctreeUnity/

    I would make a axis-aligned bounding box for the agent and only check triangles that overlap the agent bounds. You do still need to check if the agent is on the triangle. For example a navmesh triangle on a ramp could have a big AABB that overlaps the AABB of a agent ten metres in the air. Your agent is not actually on the ramp.