---
title: "Find the nearest walkable tile in C#"
url: https://recursiveai.org/onepath/cookbook/find-nearest-walkable-grid-cell/
description: "Find the nearest walkable tile to a blocked grid position in C#. Set a search radius and a unit footprint, then use the result as your pathfinding start."
version: "1.2.0"
---

# Find the nearest walkable tile 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/find-nearest-walkable-grid-cell/find-nearest-walkable-grid-cell.zip)

Use `TryFindNearestWalkable` to move a spawn off a wall. In the sample, `#` is a wall and `.` is open. `AgentSize` is the unit’s width in cells; `NearestRadius` is how far the search may look.

```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 grid = Grid.FromText(
    ".#...\n.....\n.....");
var blocked = new Cell(1, 0);
var options = new GridOptions
{
    AgentSize = 2, NearestRadius = 1
};
if (grid.TryFindNearestWalkable(
    blocked, out var start, options))
    Console.WriteLine(
        $"Start: {start.X}, {start.Y}");
```

Unzip [the example](https://recursiveai.org/onepath/cookbook/find-nearest-walkable-grid-cell/find-nearest-walkable-grid-cell.zip), then `dotnet run`.

```text
Start: 2, 0
```

A 2×2 unit at `(0, 0)` would cover the wall, so that tile isn’t picked.

If no tile fits within the radius, the call returns `false`; don’t use the `out` cell in that case.

## The full program

The download passes the recovered start and the same `GridOptions` into `FindPath`. In this map, the 2×2 unit can move from `(2, 0)` to `(3, 1)`.

```csharp
using RecursiveAI.OnePath;

var grid = Grid.FromText(".#...\n.....\n.....");
var blocked = new Cell(1, 0);
var options = new GridOptions
{
    AgentSize = 2, NearestRadius = 1
};
if (!grid.TryFindNearestWalkable(blocked, out var start, options))
{
    Console.WriteLine("No walkable start.");
    return;
}
Console.WriteLine($"Start: {start.X}, {start.Y}");

var goal = new Cell(3, 1);
var path = grid.FindPath(start, goal, options);
Console.WriteLine($"Path: {path.Status}");
```

```text
Start: 2, 0
Path: Found
```

## Variations

**Search only the original tile.** Replace the `TryFindNearestWalkable` call in the snippet at the top with this check. A radius of zero leaves no room to move off the wall in this example.

```csharp
var exact = options with { NearestRadius = 0 };
Console.WriteLine(grid.TryFindNearestWalkable(
    blocked, out _, exact));
```

```text
False
```

## Related recipes

- [Find grid paths for wide units](https://recursiveai.org/onepath/cookbook/pathfind-with-agent-clearance/)
- [Convert a world position to a grid cell](https://recursiveai.org/onepath/cookbook/convert-world-position-to-pathfinding-grid-cell/)

## The short answer

Call `Grid.TryFindNearestWalkable` with the blocked position and a `GridOptions` whose `AgentSize` is the side of the unit’s square footprint in cells. Set `NearestRadius` to limit how far the unit can snap. If the call returns `true`, pass the new start and the same options to `FindPath`. Finding a valid start and finding a route to the goal are separate decisions.

## How do you move to the closest walkable tile near an obstacle?

Set the radius in grid cells, not world units. The [grid docs](https://recursiveai.org/onepath/docs/grids/) say “`NearestRadius` bounds the search in Chebyshev rings.” One ring includes both the four edge neighbors and the four diagonal neighbors. Within that square, OnePath ranks candidates by squared straight-line distance in cell coordinates. An edge neighbor beats a diagonal neighbor if both fit. For a tie, the smaller row index wins, then the smaller column index. In the sample, `(2, 0)` and `(1, 1)` are both one cell away and both fit; `(2, 0)` is on the earlier row.

A tile being open is necessary but isn’t sufficient for a wide unit. The tile is the **top-left** of the footprint, not its center. For `AgentSize = 2`, the anchor at `(0, 0)` fails because the footprint would cover the wall at `(1, 0)`; an anchor right against the bottom edge would fail because the footprint would extend off the grid. The call also rejects candidates refused by your `Blocked` callback, if you supply one.

## What if there’s still no path to the goal?

The nearest fit is a geometric choice, not a reachability check. A wall can separate the returned tile from the goal even when there’s another valid start a little farther away. Check the path result rather than treating a successful snap as a successful route. If the gameplay rule is to stay on the unit’s side of a barrier, choose or filter spawn candidates by that rule before snapping. Increasing the radius alone does not change which of two valid tiles is nearer.
