---
title: "Find the nearest point on a navmesh in C#"
url: https://recursiveai.org/onepath/cookbook/project-point-onto-navmesh/
description: "Find the nearest point on a navigation mesh in C# with OnePath. Snap a point to a walkable surface within a maximum distance, without searching for a route."
version: "1.2.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePath) · [GitHub](https://github.com/recursive-ai-oss/onepath-csharp) · [Docs](https://recursiveai.org/onepath/docs/) · [Download example (.zip)](https://recursiveai.org/onepath/cookbook/project-point-onto-navmesh/project-point-onto-navmesh.zip)

A navigation mesh marks where characters can walk.

```shell
dotnet add package RecursiveAI.OnePath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

`RecursiveAI.OnePath` brings in OneMath, which provides `Vector3D`.

```csharp
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](https://recursiveai.org/onepath/cookbook/project-point-onto-navmesh/project-point-onto-navmesh.zip), 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:

```csharp
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:

```csharp
var above = new Vector3D(2, 1, 1);
if (mesh.TrySamplePosition(above, 2, out var ground))
    Console.WriteLine(ground);
```

```text
(2, 0, 1)
```

## Related recipes

- [Build a tiled 2D navmesh](https://recursiveai.org/onepath/cookbook/build-tiled-2d-navmesh/)
- [Run path, raycast, and nearest-point queries](https://recursiveai.org/onepath/cookbook/navmesh-queries/)
- [Filter navmesh areas and costs](https://recursiveai.org/onepath/cookbook/filter-navmesh-areas-and-costs/)

## 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.

## How far does a navmesh nearest-point query search?

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](https://recursiveai.org/onepath/docs/navmesh-queries/#nearest-polygon), 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](https://recursiveai.org/onepath/docs/navmesh-queries/) from the character’s position and check its result before moving.
