Search Unity

NavMeshQuery is causing infinite loops upon calling PathUtils.FindStraightPath

Discussion in 'Navigation' started by Abbrew, Mar 9, 2022.

  1. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    This code https://github.com/Unity-Technologi...ressTesting/Assets/Scripts/Utils/PathUtils.cs is supposed to calculate a list of waypoints based on a list of PolygonId returned by NavMeshQuery. However, it's causing an infinite loop, freezing the Editor, and forcing me to close it and restart. I've written the following code to call FindStraightPath - are there any issues with it?

    Code (CSharp):
    1. [MethodImpl(MethodImplOptions.AggressiveInlining)]
    2.             public static bool Calculate(WorldBoundsDefinitionComponent worldBounds, NavMeshQuery query, ref DynamicBuffer<ValidPathWaypointElement> waypoints, float2 startLocation, float2 endLocation)
    3.             {
    4.                 var startMapLocation =
    5.                     query.MapLocation(
    6.                         startLocation.WithY(worldBounds.Mid.y),
    7.                         worldBounds.Extents,
    8.                         HUMANOID_AGENT_TYPE,
    9.                         AREA_MASK
    10.                     );
    11.                 var endMapLocation =
    12.                     query.MapLocation(
    13.                         endLocation.WithY(worldBounds.Mid.y),
    14.                         worldBounds.Extents,
    15.                         HUMANOID_AGENT_TYPE,
    16.                         AREA_MASK
    17.                     );
    18.                 var pathBuffer =
    19.                     new NativeArray<PolygonId>(
    20.                         MAX_NUM_POLYGONS_PER_PATH,
    21.                         Allocator.Temp
    22.                     );
    23.                 var success = CalculatePath(
    24.                     startMapLocation,
    25.                     endMapLocation,
    26.                     query,
    27.                     ref pathBuffer
    28.                 );
    29.  
    30.                 waypoints.Clear();
    31.  
    32.                 if (success)
    33.                 {
    34.                     var polygonPathValid = new NativeList<PolygonId>(Allocator.Temp);
    35.                     for (var k = 0; k < MAX_NUM_POLYGONS_PER_PATH; k++)
    36.                     {
    37.                         var polygon = pathBuffer[k];
    38.                         if (!polygon.Equals(default))
    39.                         {
    40.                             polygonPathValid.Add(polygon);
    41.                             Debug.Log(query.GetPolygonType(polygon));
    42.                         }
    43.                     }
    44.                     var polygonPathValidArray = polygonPathValid.AsArray();
    45.                     var numValidPolygons = polygonPathValidArray.Length;
    46.  
    47.                     var straightPath = new NativeArray<NavMeshLocation>(MAX_WAYPOINTS_PER_PATH, Allocator.Temp);
    48.                     var straightPathFlags = new NativeArray<StraightPathFlags>(MAX_WAYPOINTS_PER_PATH, Allocator.Temp);
    49.                     var vertexSides = new NativeArray<float>(MAX_WAYPOINTS_PER_PATH, Allocator.Temp);
    50.                     var straightPathLength = 0;
    51.  
    52.                     PathUtils.FindStraightPath(
    53.                         query,
    54.                         startLocation.WithY(),
    55.                         endMapLocation.position,
    56.                         polygonPathValidArray,
    57.                         numValidPolygons,
    58.                         ref straightPath,
    59.                         ref straightPathFlags,
    60.                         ref vertexSides,
    61.                         ref straightPathLength,
    62.                         MAX_WAYPOINTS_PER_PATH
    63.                     );
    64.  
    65.                     for (var i = 0; i < straightPathLength; i++)
    66.                     {
    67.                         waypoints.Add(new ValidPathWaypointElement
    68.                         {
    69.                             to = straightPath[i].position.AsFloat3().xz
    70.                         });
    71.                     }
    72.                 }
    73.  
    74.                 return success;
    75.             }
    76.  
    77.             [MethodImpl(MethodImplOptions.AggressiveInlining)]
    78.             private static bool CalculatePath(
    79.                 NavMeshLocation start,
    80.                 NavMeshLocation end,
    81.                 NavMeshQuery query,
    82.                 ref NativeArray<PolygonId> pathBuffer
    83.             )
    84.             {
    85.                 var status = query.BeginFindPath(start, end, AREA_MASK);
    86.  
    87.                 while(true)
    88.                 {
    89.                     switch (status)
    90.                     {
    91.                         case PathQueryStatus.InProgress:
    92.                             status = query.UpdateFindPath(MAX_ITERATIONS_PER_PATH, out var _);
    93.                             break;
    94.  
    95.                         case PathQueryStatus.Success:
    96.                             query.EndFindPath(out var _);
    97.                             query.GetPathResult(pathBuffer);
    98.                             return true;
    99.  
    100.                         case PathQueryStatus.Failure:
    101.                             return false;
    102.  
    103.                         default:
    104.                             return false;
    105.                     }
    106.                 }
    107.             }
    108.         }
    I'm also using NavMeshComponents to generate the NavMesh