---
title: "Raycast along a walkable navmesh in C#"
url: https://recursiveai.org/onepath/cookbook/raycast-on-navmesh/
description: "Raycast along a walkable navmesh surface in C# with OnePath. Check whether a straight route is blocked and find the first boundary it crosses."
version: "1.2.0"
---

# Raycast along a walkable 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/raycast-on-navmesh/raycast-on-navmesh.zip)

`NavMesh.Raycast` tests straight-line walking, not 3D collisions. In the map, `#` marks blocked cells; columns map to X, rows to Z, and the floor is at Y = 0.

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

```csharp
using RecursiveAI.OnePath;

var map = Grid.FromText("""
    ....####
    ....####
    ....####
    ....####
    ........
    ........
    ........
    ........
    """);
var mesh = NavMesh.Build2D(map);
bool blocked = mesh.Raycast(new(2, 0, 2),
    new(6, 0, 6), out var hit);
Console.WriteLine(
    blocked ? "Blocked" : "Clear");
```

```text
Blocked
```

Unzip [the example](https://recursiveai.org/onepath/cookbook/raycast-on-navmesh/raycast-on-navmesh.zip), then `dotnet run`.

## The full program

The diagonal crosses the blocked corner of the map. The second cast goes straight down the left side and stays on walkable ground.

```csharp
using RecursiveAI.OnePath;

var map = Grid.FromText("""
    ....####
    ....####
    ....####
    ....####
    ........
    ........
    ........
    ........
    """);
var mesh = NavMesh.Build2D(map);
bool blocked = mesh.Raycast(new(2, 0, 2),
    new(6, 0, 6), out var hit);
Console.WriteLine(
    blocked ? "Blocked" : "Clear");
if (blocked)
    Console.WriteLine($"Boundary: {hit.Position}");

bool clearRouteBlocked = mesh.Raycast(new(2, 0, 2),
    new(2, 0, 6), out _);
Console.WriteLine(clearRouteBlocked ? "Blocked" : "Clear");
```

```text
Blocked
Boundary: (3.400390625, 0, 3.400390625)
Clear
```

The hit is on the baked mesh boundary, not the edge of a raw grid cell. If the start has no nearby walkable polygon, `NavMesh.Raycast` also returns `true`, but reports the start itself instead of a boundary.

## Variations

To check that the start actually snaps to walkable ground before casting, replace the cast and print lines after building `mesh`. This uses a one-unit snap distance.

```csharp
if (mesh.TrySamplePosition(new(2, 0, 2), 1,
    out var start))
{
    bool blocked = mesh.Raycast(start,
        new(2, 0, 6), out _);
    Console.WriteLine(
        blocked ? "Blocked" : "Clear");
}
else
{
    Console.WriteLine("No nearby walkable ground");
}
```

```text
Clear
```

## Related recipes

- [Find straight-path corners in a navmesh corridor](https://recursiveai.org/onepath/cookbook/find-navmesh-straight-path-corners/)
- [Find the distance to the nearest navmesh wall](https://recursiveai.org/onepath/cookbook/find-nearby-navmesh-walls/)

## The short answer

Call `NavMesh.Raycast` with a start and a destination in world coordinates. `true` means the straight walk is blocked; `false` means it stays on the walkable mesh. Use the reported position when the cast hits a boundary. A clear result is useful when you want to skip a turn and walk directly to a later waypoint. A blocked result rules out only the straight segment, not a route around the obstacle.

## How do you raycast against only the navmesh?

The [navmesh raycast](https://recursiveai.org/onepath/docs/navmesh-queries/#raycast) tracks the line in X/Z. It starts in one polygon, finds the edge where the line leaves it and continues if that edge leads into an allowed neighbor. A gap, an outside edge or a polygon rejected by the query filter stops the cast. It does not follow off-mesh connections, such as links added to represent jumps.

This is different from firing a 3D ray downward to pick a point or testing whether a wall blocks vision. The cast does not test heights along the line and knows nothing about objects absent from the navmesh. A mesh with stacked floors needs the correct starting polygon; the target’s Y coordinate does not make the cast jump to the floor above. Use a 3D collision query when an object’s actual shape or height matters.

## Why is a navmesh raycast blocked when both points are walkable?

The points can be walkable individually even if the segment between them cuts across missing ground. That is what happens in the diagonal example. Checking the destination alone would miss the gap. If the segment is blocked but you still need to reach the destination, run a path search and follow the returned waypoints instead of forcing a straight walk.

Also check how your start was chosen. The convenience method searches for a nearby polygon in a box with half-extents `(2, 4, 2)` before casting. Where that snap is too broad, use `TrySamplePosition` with a smaller distance as shown above. If you already know which polygon contains the start, use `NavMeshQuery.Raycast` with that polygon reference. The lower-level query returns a status separately from the hit, so a stale polygon reference or a start outside the polygon is distinguishable from an actual blocked segment.
