---
title: "Grid paths for wide units in C#"
url: https://recursiveai.org/onepath/cookbook/pathfind-with-agent-clearance/
description: "Find grid paths for 1×1 and 2×2 units in C#. Use OnePath’s true clearance to route a larger unit through a wider opening on the same map."
version: "1.2.0"
---

# Grid paths for wide units 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/pathfind-with-agent-clearance/pathfind-with-agent-clearance.zip)

Here `#` is a wall and `.` is open.

```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(
    "..#..\n.....\n..#..\n..#..");
// Cell(x, y): column, row
Cell start = new(0, 0), goal = new(3, 0);
foreach (int size in new[] { 1, 2 }) {
    var opts = GridOptions.ForAgentSize(size);
    var path = map.FindPath(start, goal, opts);
    Console.WriteLine(
        $"{size}x{size}: {path.Status}");
}
```

```text
1x1: Found
2x2: NoPath
```

Unzip [the example](https://recursiveai.org/onepath/cookbook/pathfind-with-agent-clearance/pathfind-with-agent-clearance.zip), then `dotnet run`.

## The full program

The wall in column 2 now has two openings: row 1 is a single gap, and rows 3 and 4 form a two-cell gap. The searches share their start and goal; the program then closes row 4 and runs both again.

```csharp
using RecursiveAI.OnePath;

// One gap at row 1; two consecutive open rows at 3 and 4.
var map = Grid.FromText("..#..\n.....\n..#..\n.....\n.....");
Cell start = new(0, 0), goal = new(3, 0);
foreach (int size in new[] { 1, 2 })
{
    var options = GridOptions.ForAgentSize(size);
    GridPath path = map.FindPath(start, goal, options);
    string route = string.Join(" -> ", path.Cells.ToArray()
        .Select(cell => $"({cell.X},{cell.Y})"));
    Console.WriteLine($"{size}x{size}: {path.Status}, {route}");
}

map.SetArea(2, 4, AreaCosts.Wall);
Console.WriteLine("After closing the wide gap:");
foreach (int size in new[] { 1, 2 })
{
    var options = GridOptions.ForAgentSize(size);
    Console.WriteLine($"{size}x{size}: {map.FindPath(start, goal, options).Status}");
}
```

`dotnet run` prints:

```text
1x1: Found, (0,0) -> (1,1) -> (2,1) -> (3,1) -> (3,0)
2x2: Found, (0,0) -> (0,1) -> (0,2) -> (0,3) -> (1,3) -> (2,3) -> (3,3) -> (3,2) -> (3,1) -> (3,0)
After closing the wide gap:
1x1: Found
2x2: NoPath
```

The 1×1 unit crosses at row 1. The 2×2 unit crosses at row 3, with its second row in row 4. Once the wide opening is closed, the larger unit gets `NoPath`; if the footprint can’t fit at an endpoint, the status is `StartInvalid` or `GoalInvalid` instead.

## Variations

**Check whether a unit fits at a cell.** Add this to the snippet at the top. The opening has clearance `1`, so it fits a 1×1 unit but not a 2×2 unit.

```csharp
Console.WriteLine(map.GetClearance(2, 1));
```

```text
1
```

## Related recipes

- [Stop diagonal corner cutting in C#](https://recursiveai.org/onepath/cookbook/prevent-diagonal-corner-cutting/)
- [A* pathfinding in C#](https://recursiveai.org/onepath/cookbook/a-star-pathfinding/)
- [Find a weighted grid path in C#](https://recursiveai.org/onepath/cookbook/route-around-expensive-grid-terrain/)

## The short answer

Use `GridOptions.ForAgentSize(2)` with `Grid.FindPath` to route a 2×2 unit on a square grid. Pass `1` for a single-cell unit on the same map. The size is a side length in cells, not a radius, and the supported range is 1 through 255. Hex grids support only size 1.

## How do you find paths for different-sized units on one grid?

Create the grid once. For each unit, pass options for its footprint when you call `FindPath`. The floor plan stays the same: in the example, the same one-cell opening is a legal route for a small unit and an obstacle for a large one. You don’t need to mark that opening as a wall on a second map.

[OnePath’s clearance rule](https://github.com/recursive-ai-oss/onepath-csharp/blob/9da11631e6c0d85abbb302e51f0511b74fa728fd/docs/grids.md#L61-L70) anchors the footprint at its top-left cell and extends it right and down. At position `(3,0)`, a 2×2 unit covers columns 3 and 4 and rows 0 and 1. Clearance is the size of the largest open square anchored there, so the same lookup works for any supported size. Cells outside the map count as walls: a cell at the far right can’t anchor a 2×2 square even if it isn’t a wall itself.

## Why can a wide unit fail to reach an open tile?

Check the endpoints before changing the route. A goal tile can be open while another tile in the goal footprint is blocked. `GetClearance` distinguishes that case from a bad passage: compare each endpoint’s clearance with the unit size. If both endpoints fit but there’s no chain of valid positions between them, the search reports `NoPath`. An opening has to fit the whole footprint at every step, not just the anchor.

For diagonal moves, the corner rule also checks footprints, not single tiles. Under `NoCornerCutting`, both positions beside the diagonal must fit the unit. That’s why a wide unit can be able to stand at both ends of a diagonal but still need to go around. The model is a fixed square aligned to the grid; it doesn’t turn a rectangle sideways to fit it through a doorway.
