Recursive AI

Find the nearest point on a navmesh in C#

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

A navigation mesh marks where characters can walk.

Terminal
dotnet add package RecursiveAI.OnePath

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

RecursiveAI.OnePath brings in OneMath, which provides Vector3D.

C#
using RecursiveAI.OneMath;
using RecursiveAI.OnePath;
 
var grid = Grid.FromText("....\n....");
var mesh = NavMesh.Build2D(grid);
var outside = new Vector3D(-1, 0, 1);
if (mesh.TrySamplePosition(
    outside, 2, out var point))
    Console.WriteLine(point);
else
    Console.WriteLine("No point in range.");

Unzip the example, then dotnet run.

Prints the snapped (X, Y, Z) point:

Text
(0.599609375, 0, 1)

The two rows of dots are walkable cells on the X/Z plane. The mesh is inset about 0.6 units from the grid edge to leave clearance, so the point snaps to X ≈ 0.6, not X = 0. TrySamplePosition returns false if no point is in range; don’t use the output point then.

Variations

Reject points that are too far away. With the same mesh and outside from the snippet at the top, a one-unit limit doesn’t reach the walkable edge:

C#
Console.WriteLine(mesh.TrySamplePosition(outside, 1, out _));
Text
False

Project down onto the walkable surface. On the same mesh, start above the center of the grid rather than outside its edge:

C#
var above = new Vector3D(2, 1, 1);
if (mesh.TrySamplePosition(above, 2, out var ground))
    Console.WriteLine(ground);
Text
(2, 0, 1)

The short answer

Call mesh.TrySamplePosition(position, maxDistance, out var point) on a OnePath NavMesh. On success, point is a point on the walkable surface, not a polygon center. maxDistance limits the 3D distance between the input and result. This query does not calculate a route.

OnePath uses the distance as the half-width of a search box along X, Y and Z. The query considers polygons whose bounds overlap that box, then checks that the chosen point is actually within the requested 3D distance. A polygon in the box isn’t enough by itself: a point near a box corner could be farther away than the distance allows.

Choose a limit based on how far you’re willing to move the input, not the width of the level. For example, a click just beside a walkway can snap onto it, but a click several units away can stay invalid. Check the Boolean result instead of treating (0, 0, 0) as a failure marker; the origin can be a valid point on another mesh.

Which floor does the nearest navmesh point land on?

As described in Navigation mesh queries, if your position lies inside a polygon’s outline viewed from above, the candidate keeps X and Z and takes its Y from the surface. Outside the outline, the candidate is on the nearest edge in X/Z, with the height taken along that edge. OnePath compares the resulting candidates by squared 3D distance. When walkable floors overlap in plan view, the nearer floor vertically wins if both qualify. Don’t assume the query will prefer the floor below the point.

Can a character reach the point after it snaps to the navmesh?

Not necessarily. The nearby surface could be on the other side of a wall or on a disconnected patch. A point projection takes no starting position, so it can’t determine whether your character can get there. Use TrySamplePosition alone to place a marker on the mesh. If you mean to send a character there, follow the projection with a path query from the character’s position and check its result before moving.

All onepath recipes