Recursive AI

Handle a partial navmesh path in C#

OnePath is a free, open-source pathfinding library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

If IsPartial is true, stop at the last waypoint instead of marking the goal reached.

Terminal
dotnet add package RecursiveAI.OnePath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
// Use the query and inputs below.
var path = query.FindPath(
    start, goal, options);
if (path.IsPartial && path.Points.Count > 0)
    Console.WriteLine(path.Points[^1].Position);

The example is the full program below: unzip it and dotnet run.

The last waypoint is (2, 0, 1), not the requested destination.

The full program

The example builds three adjoining squares. It starts at (1, 0, 1) in the first square and asks for (5, 0, 1) in the last one. The filter excludes the middle square (flag 2), so the end square exists but can’t be reached.

C#
using RecursiveAI.OneMath;
using RecursiveAI.OnePath;
using RecursiveAI.OnePath.Mesh;
using RecursiveAI.OnePath.Mesh.Queries;
 
var query = MakeCorridor();
var start = new Vector3D(1, 0, 1);
var goal = new Vector3D(5, 0, 1);
var options = new PathOptions
{
    Filter = new QueryFilter { ExcludeFlags = 2 }
};
var path = query.FindPath(start, goal, options);
Console.WriteLine(path.Status);
if (path.IsPartial && path.Points.Count > 0)
{
    var stop = path.Points[^1].Position;
    Console.WriteLine(FormattableString.Invariant(
        $"Stop at ({stop.X}, {stop.Y}, {stop.Z}); goal not reached."));
}
 
static NavMeshQuery MakeCorridor()
{
    var settings = new NavMeshParams
    {
        TileSize = 8, CellSize = 0.25,
        MaxVertsPerPoly = 6, WalkableClimb = 0.5f
    };
    double[] vertices =
    [
        0, 0, 0,  2, 0, 0,  4, 0, 0,  6, 0, 0,
        0, 0, 2,  2, 0, 2,  4, 0, 2,  6, 0, 2
    ];
    var tile = NavTileAssembler.FromPolygons(settings, 0, 0, 0, 0,
        vertices, [4, 4, 4], [0, 1, 5, 4, 1, 2, 6, 5, 2, 3, 7, 6],
        [0, 0, 0], // Area IDs
        [1, 2, 1]); // Polygon flags
    var mesh = new TiledNavMesh(settings);
    mesh.AddTile(NavTile.Create(tile, settings));
    return new NavMeshQuery(mesh);
}
Text
Partial
Stop at (2, 0, 1); goal not reached.

If the query can’t find an allowed polygon near the start or goal, it reports StartInvalid or GoalInvalid instead of returning this best-effort path.

Variations

Let the character cross the middle square by removing the filter. In the example, the status changes to Found:

C#
var open = query.FindPath(start, goal);
Console.WriteLine(open.Status);

The short answer

Check IsPartial on the PathResult from NavMeshQuery.FindPath. If it’s true and you have waypoints, follow them and stop at the last one without marking the requested goal as reached. If your character must arrive at the goal or not move at all, accept only Found. HasPath is true for both Found and Partial; it’s not an arrival check.

Why does a navmesh query return a partial path?

A valid start and a valid goal don’t guarantee there’s a connection between them. OnePath first looks for an allowed polygon near each position. If both exist, the search tries to connect them. An excluded area, as in the example, can break that connection. Reaching the search’s node limit can also produce a partial result, so Partial alone doesn’t tell you which cause applies. Check the filter and the query settings before deciding whether the underlying map needs to change.

A partial result differs from an invalid goal. In this example the goal polygon is allowed and nearby, but the middle polygon can’t be crossed. If the query can’t find an allowed polygon near the requested destination in the first place, it gives GoalInvalid. In that case there is no partial route to follow. The same distinction applies to StartInvalid at the other end.

Where does a partial navmesh path end?

OnePath uses the last polygon of the partial corridor, meaning the last area of walkable surface in the route it found. It projects the snapped goal onto that polygon and runs the straight-path query toward that point. The example reaches the edge of the first square at X = 2; walking in a straight line from there to X = 5 would cross the forbidden square.

Use Points for world-space movement and Corridor when you need polygon references. Keep the goal separate from the waypoint list so that reaching the last point never silently turns a partial route into success. Check that points exist before indexing, and check the result’s truncation flags if you set a small corridor or point limit. A returned waypoint at the end of a truncated list need not be the end of the route the search found.

All onepath recipes