Recursive AI

Navmesh distance to wall and nearby walls in C#

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

A navmesh wall is a boundary with no allowed walkable polygon beyond it. Query nearby segments to measure clearance or draw local boundaries.

Terminal
dotnet add package RecursiveAI.OnePath

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

C#
using RecursiveAI.OneMath;
using RecursiveAI.OnePath.Mesh;
using RecursiveAI.OnePath.Mesh.Queries;
 
// room.nav is in the download.
var mesh = NavMeshSnapshot.Restore(
    File.ReadAllBytes("room.nav"));
var query = new NavMeshQuery(mesh);
var center = new Vector3D(3, 0, 2);
var poly = query.FindNearestPoly(center,
    new Vector3D(1, 1, 1)).Poly;
var hit = query.DistanceToWall(poly, center,
    3, null, out var status);
Console.WriteLine($"{status}: {hit.Found}, {hit.Distance:F2}");
Text
Success: True, 1.00

Unzip the example, then dotnet run.

The full program

The program constructs room.nav from the four corners below, then prints the nearest wall and every segment within three units. The same generated mesh is included in the download for the snippet at the top.

C#
using RecursiveAI.OneMath;
using RecursiveAI.OnePath.Mesh;
using RecursiveAI.OnePath.Mesh.Queries;
 
// A single flat rectangle: X=1..11, Z=1..5, Y=0.
var settings = new NavMeshParams();
var tile = NavTileAssembler.FromPolygons(settings, 0, 0, 0, 0,
    [1,0,1, 11,0,1, 11,0,5, 1,0,5], [4], [0,1,2,3]);
var mesh = new TiledNavMesh(settings);
mesh.AddTile(NavTile.Create(tile, settings));
File.WriteAllBytes("room.nav", NavMeshSnapshot.Take(mesh));
var query = new NavMeshQuery(mesh);
var center = new Vector3D(3, 0, 2);
var nearest = query.FindNearestPoly(center, new Vector3D(1, 1, 1));
var hit = query.DistanceToWall(nearest.Poly, center, 3, null, out var status);
Console.WriteLine($"{status}: found={hit.Found}, distance={hit.Distance:F2}");
Console.WriteLine($"point: {hit.Point}; normal: {hit.Normal}");
var walls = new WallSegment[8];
var wallStatus = query.FindWalls(nearest.Poly, center, 3, null,
    walls, out int count, out bool truncated);
Console.WriteLine($"{wallStatus}: {count} wall segments, truncated={truncated}");
foreach (var wall in walls.AsSpan(0, count))
    Console.WriteLine($"  ({wall.AX},{wall.AY},{wall.AZ}) -> ({wall.BX},{wall.BY},{wall.BZ})");
Text
Success: found=True, distance=1.00
point: (3, 0, 1); normal: (0, 0, 1)
Success: 3 wall segments, truncated=False
  (1,0,1) -> (11,0,1)
  (1,0,5) -> (1,0,1)
  (11,0,5) -> (1,0,5)

The rectangle has corners (1,0,1), (11,0,1), (11,0,5) and (1,0,5). The point (3,0,2) is one unit from its bottom wall. FindWalls returns whole wall pieces within range, not pieces clipped to the search circle: the first segment is ten units long.

Check the returned status and truncated before using the array; invalid or filter-rejected starting polygons return InvalidStartPoly rather than throwing.

Variations

To test only a half-unit clearance, replace the query after poly in the snippet at the top:

C#
var hit = query.DistanceToWall(poly, center,
    0.5, null, out var status);
Console.WriteLine($"{status}: {hit.Found}, {hit.Distance:F2}");
Text
Success: False, 0.50

No wall was found inside that radius. The returned distance is the radius, not a measured wall distance.

The short answer

Use NavMeshQuery.DistanceToWall for the nearest boundary and FindWalls for every nearby boundary segment. Both take a starting polygon, a world-space center, a radius and an optional query filter. Find the starting polygon with FindNearestPoly before making the wall query. The example uses a rectangle so the answer is easy to check: the center is one unit above its bottom edge.

These queries measure distance in the X/Z plane. They are useful for local clearance around a character’s position, but they do not measure distance to arbitrary scene triangles, ceilings or physics colliders. A mesh boundary is the input, not the visual wall of a level. If the mesh was built with a radius inset, the boundary already reflects that inset.

How do you find the distance to a navmesh wall in C#?

DistanceToWall searches reachable polygons near the center, scans their boundary pieces and chooses the nearest point within the radius. Its result includes Found, Distance, Point, Normal, the owning polygon and its edge index.

The normal points from the hit toward the center in X/Z, with zero Y. If the center is exactly on a wall, the result uses that edge’s inward normal instead. This differs from a direction you supply to a raycast: the wall query searches around the point, so you do not need to guess which direction is closest.

Always inspect both the query status and Found. A successful query can still find no wall. In that case Distance equals your search radius and the point is not a hit location. Treat that as “no boundary within this range,” not “the nearest boundary is exactly this far away.” Increase the range if you need a farther boundary.

How do you get nearby navmesh wall segments?

Supply a WallSegment array to FindWalls. Only its first count entries contain results from this call. Each segment stores world-space A and B endpoints, a polygon and an edge index. Results are ordered by distance; if the array is too small, the nearest pieces are retained and truncated is true. Increase the buffer when your use needs every piece rather than just the nearest few.

The segment endpoints follow the polygon’s winding: the walkable side is to the left of A → B in X/Z. Endpoints preserve interpolated edge heights, even though proximity is tested horizontally. On meshes with reachable stacked floors, compare endpoint heights yourself if your drawing or steering logic should use only one floor.

A query filter changes what counts as a wall. An edge shared with a rejected polygon becomes a boundary. On a tile border, only the stretches not covered by links to accepted polygons are walls. Off-mesh links are not followed by the circle search behind these queries. This makes a wall list specific to the character’s movement permissions rather than a fixed list of every authored polygon edge. The navmesh query documentation gives the exact wall-piece and tie-breaking rules.

All onepath recipes