Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

How to use NavMeshQuery get path points?

Discussion in 'AI & Navigation Previews' started by wang37921, Mar 19, 2019.

  1. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    92
    NavMeshQuery.GetPathResult only return the PolygonID list.
    using the PolygonID list and NavMeshQuery.GetPortalPoints, I can get a corner list.
    In a simple line nav request, I got a corner list: just the begin point and end point.

    upload_2019-3-19_11-16-10.png

    If I move agent along the corner list. The agent will move along the red path, go through the bridge, losing contact with the navmesh surface.

    I tried the NavMeshAgent, it will along the green line move. That's the result I want.

    I have checked the NavMeshAgent:CalculatePath, the return path's corner, is same as the calc from PolygonID list and GetPortalPoints, just begin and end point.

    I want insert corner line with portal points, but I found Polygon maybe not a plane:

    upload_2019-3-19_11-11-54.png
    Also losing contact with the navmesh surface, in the curved surface/
    upload_2019-3-19_11-18-58.png

    So, I want to know How i can got a path points like the first diagrammatic's green path.
     
    Last edited: Mar 19, 2019
  2. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    92
    Maybe I need the NavMeshQuery:MoveLocation to interpolation between the corners.
     
  3. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    129
    Currently, you have to do a bit of work to calculate the path yourself from the polygons.

    Here's an example working in 2019.1.0b7. You'll need to include the Mathematics preview package for it to compile.

    https://gist.github.com/jeffvella/80181b42ae4ea6596a973586ec3ed17f



    Here is the relevant part of the code I wrote quickly for main-thread/blocking/non-jobs use to illustrate. It uses some code by MikkoMononen/Unity/Zulfa Juniadi for the method PathUtils.FindStraightPath() which i found here: https://github.com/zulfajuniadi/uni...r/Assets/NavJob/Systems/NavMeshQuerySystem.cs

    Code (CSharp):
    1.     bool TryFindPath(Vector3 start, Vector3 end, int agentId, float agentRadius, int areas, out Vector3[] path)
    2.     {
    3.         const int maxPathLength = 100;
    4.  
    5.         using (var result = new NativeArray<PolygonId>(100, Allocator.Persistent))
    6.         using (var query = new NavMeshQuery(NavMeshWorld.GetDefaultWorld(), Allocator.Persistent, 100))
    7.         {
    8.             var from = query.MapLocation(start, Vector3.one * 10, 0);
    9.             var to = query.MapLocation(end, Vector3.one * 10, 0);
    10.             var status = query.BeginFindPath(from, to, areas);
    11.             int maxIterations = 1024;
    12.  
    13.             while (true)
    14.             {
    15.                 switch (status)
    16.                 {
    17.                     case PathQueryStatus.InProgress:
    18.                         status = query.UpdateFindPath(maxIterations, out int currentIterations);
    19.                         break;
    20.  
    21.                     case PathQueryStatus.Success:
    22.  
    23.                         var finalStatus = query.EndFindPath(out int pathLength);
    24.                         var pathResult = query.GetPathResult(result);
    25.                         var straightPath = new NativeArray<NavMeshLocation>(pathLength, Allocator.Temp);
    26.                         var straightPathFlags = new NativeArray<StraightPathFlags>(pathLength, Allocator.Temp);
    27.                         var vertexSide = new NativeArray<float>(pathLength, Allocator.Temp);
    28.  
    29.                         try
    30.                         {
    31.                             int straightPathCount = 0;
    32.                             var pathStatus = PathUtils.FindStraightPath(query, start, end, result, pathLength, ref straightPath, ref straightPathFlags, ref vertexSide, ref straightPathCount, maxPathLength);
    33.                             if (pathStatus == PathQueryStatus.Success)
    34.                             {
    35.                                 path = new Vector3[straightPathCount];
    36.                                 for (int i = 0; i < straightPathCount; i++)
    37.                                 {
    38.                                     path[i] = straightPath[i].position;
    39.                                 }
    40.                                 return true;
    41.                             }
    42.  
    43.                             path = default;
    44.                             Debug.Log($"Nav query failed with the status: {status}");
    45.                             return false;
    46.                         }
    47.                         finally
    48.                         {
    49.                             straightPath.Dispose();
    50.                             straightPathFlags.Dispose();
    51.                             vertexSide.Dispose();
    52.                         }
    53.  
    54.                     case PathQueryStatus.Failure:
    55.                         path = default;
    56.                         return false;
    57.  
    58.                     default:
    59.                         Debug.Log($"Nav query failed with the status: {status}");
    60.                         path = default;
    61.                         return false;
    62.                 }
    63.             }
    64.         }
    65.     }
     
    Last edited: Mar 23, 2019
    thelebaron and proepkess like this.
  4. dudleyhk

    dudleyhk

    Joined:
    Sep 27, 2017
    Posts:
    10
    LoxodonStudio and wang37921 like this.
unityunity