FindStraightPath takes a corridor: the ordered list of walkable polygons a path search returns. The download’s bend.nav has three squares in an L.
dotnet add package RecursiveAI.OnePathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OnePath; using RecursiveAI.OnePath.Mesh; using RecursiveAI.OnePath.Mesh.Queries; var mesh = NavMeshSnapshot.Restore( File.ReadAllBytes("bend.nav")); var tile = mesh.GetTileRef(0, 0, 0); var corridor = new[] { 0, 1, 2 } .Select(i => mesh.GetPolyRef(tile, i)) .ToArray(); var points = new StraightPathPoint[8]; var query = new NavMeshQuery(mesh); var status = query.FindStraightPath( new(2, 0, 4), new(6, 0, 8), corridor, points, out int count, out bool truncated); if (status != QueryStatus.Success || truncated) throw new InvalidOperationException( $"Incomplete straight path: {status}"); foreach (var point in points.AsSpan(0, count)) Console.WriteLine(point.Position);
(2, 0, 4) (5, 0, 5) (6, 0, 8)
If you have only a start and a goal, use FindPath: its result already contains the waypoints in Points.
Unzip the example, then dotnet run.
The full program
Seen from above, the sample has square 2 above 1 along positive Z, and square 0 to the left of 1. The squares are four units on a side. The corridor is 0 → 1 → 2; the inner corner is at X=5, Z=5. The full program also prints each waypoint’s flags.
using RecursiveAI.OnePath; using RecursiveAI.OnePath.Mesh; using RecursiveAI.OnePath.Mesh.Queries; var mesh = NavMeshSnapshot.Restore( File.ReadAllBytes("bend.nav")); var tile = mesh.GetTileRef(0, 0, 0); var corridor = new[] { 0, 1, 2 } .Select(i => mesh.GetPolyRef(tile, i)) .ToArray(); var points = new StraightPathPoint[8]; var query = new NavMeshQuery(mesh); var status = query.FindStraightPath( new(2, 0, 4), new(6, 0, 8), corridor, points, out int count, out bool truncated); if (status != QueryStatus.Success || truncated) throw new InvalidOperationException( $"Incomplete straight path: {status}"); foreach (var point in points.AsSpan(0, count)) Console.WriteLine($"{point.Position} {point.Flags}");
(2, 0, 4) Start (5, 0, 5) None (6, 0, 8) End
An empty corridor, stale polygon references or disconnected consecutive polygons give InvalidCorridor; if truncated is true, do not treat the output as a complete route.
Variations
For a steering update that needs only the start and the next point, call the overload with a two-point limit. Replace the FindStraightPath call and the print loop in the example with:
var status = query.FindStraightPath( new(2, 0, 4), new(6, 0, 8), corridor, points, 2, out int count, out bool truncated); Console.WriteLine($"{status}, partial: {truncated}"); foreach (var point in points.AsSpan(0, count)) Console.WriteLine(point.Position);
Success, partial: True (2, 0, 4) (5, 0, 5)
partial: True appears because reaching the two-point limit sets truncated.
Related recipes
The short answer
Use NavMeshQuery.FindStraightPath when you already have a polygon corridor. Give it the start and end positions, the polygon references in traversal order and a waypoint buffer. Read only the first count entries. In the sample, three polygons produce three points: the start, a single turn at the inside of the bend and the destination. There is no general rule that one polygon produces one waypoint.
How do you get the waypoints of a navmesh path?
Use FindStraightPath separately if you have stored a corridor and want to compute new waypoints within it, for example after a character moves inside its starting polygon. It will not replace the corridor with a different route.
The straight-path query clamps the endpoints to the first and last polygons. It uses the funnel algorithm, also called string pulling, to find the shortest line through that corridor in the horizontal plane. Each shared opening constrains how far a straight segment can continue. Where the line can no longer pass through the next opening, it adds a corner. That is why the sample turns at the edge of the open space instead of walking to the center of each square. The corner height comes from the edge where it lies.
Why is my navmesh corner list empty or incomplete?
Check the status before reading the points. Polygon references belong to the mesh that produced them and can become stale when a tile is removed or replaced. Rerun the corridor search after such a change if the query returns InvalidCorridor. If the status is Success but the buffer filled, increase its capacity when you need the whole route. A point limit also sets truncated when reached, even if the final point happened to fit; the status alone does not tell you whether the destination was emitted.
A path with no interior turns may contain only its endpoints. Coincident points are merged and their flags combined, so read the flags as flags rather than assuming one marker per entry. With an off-mesh connection, the query emits the link entry and exit and marks the entry OffMeshConnection. Use that marker to switch movement behavior instead of trying to walk the link like an ordinary polygon edge.