---
title: "Limit crowd avoidance neighbor count in C#"
url: https://recursiveai.org/onepath/cookbook/tune-crowd-neighbor-count/
description: "Set a per-agent neighbor cap for local crowd avoidance in C#. See how the cap changes a crowded step and how to select a navmesh crowd preset."
version: "1.2.0"
---

# Limit crowd avoidance neighbor count 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/tune-crowd-neighbor-count/tune-crowd-neighbor-count.zip)

Set a custom cap in ORCA, OnePath’s local collision-avoidance solver, or use a navmesh crowd preset.

```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.Crowds.Orca;
var walker = new OrcaAgent();
walker.MaxNeighbors = 4;
Console.WriteLine(walker.MaxNeighbors);
```

Unzip [the example](https://recursiveai.org/onepath/cookbook/tune-crowd-neighbor-count/tune-crowd-neighbor-count.zip), then `dotnet run`.

Prints:

```text
4
```

## The full program

`RecursiveAI.OnePath` brings in `RecursiveAI.OneMath` for `Vector2D`; no separate install is needed.

```csharp
using RecursiveAI.OneMath;
using RecursiveAI.OnePath.Crowds.Orca;

foreach (int cap in new[] { 2, 4, 8 })
{
    var sim = new OrcaSimulator();
    var walker = new OrcaAgent
    {
        Position = Vector2D.Zero,
        PreferredVelocity = new(1, 0),
        MaxSpeed = 1,
        Radius = 0.25,
        MaxNeighbors = cap,
        NeighborDistance = 4,
        TimeHorizon = 2,
        ObstacleTimeHorizon = 2,
    };
    sim.AddAgent(walker);

    // Two closer bystanders, then a blocker straight ahead and a row behind it.
    Vector2D[] positions = [new(0, -0.75), new(0, 0.75), new(1, 0),
        new(1.5, -0.75), new(1.5, 0.75), new(2, 0), new(2.5, -0.75), new(2.5, 0.75)];
    foreach (Vector2D position in positions)
    {
        var bystander = walker; // OrcaAgent is a struct, so this makes a copy.
        bystander.Position = position;
        bystander.MaxSpeed = 0;
        bystander.PreferredVelocity = Vector2D.Zero;
        sim.AddAgent(bystander);
    }

    sim.Step(0.1);
    Vector2D velocity = sim[0].Velocity;
    Console.WriteLine($"Cap {cap}: forward motion reduced = {velocity.X < walker.PreferredVelocity.X}");
}
```

```text
Cap 2: forward motion reduced = False
Cap 4: forward motion reduced = True
Cap 8: forward motion reduced = True
```

The full program adds the search distance and required look-ahead settings, then steps a walker toward eight stationary agents. The two closest are beside the walker; the third closest is directly ahead. A cap of two leaves the blocker out. With four or eight, the blocker is considered and the walker’s forward motion is reduced on that step. The setup and the look-ahead stay the same across the three runs.

A negative cap is invalid: `AddAgent` throws `ArgumentException`. Zero skips other agents, but doesn’t turn off static obstacle avoidance in the standalone solver.

## Variations

**Use a preset on a navigation mesh.** Set `Quality` on the options you pass to `Crowd.Add`. `Low` allows four neighbors; `Medium` allows eight and `High` allows twelve.

```csharp
using RecursiveAI.OnePath;

var options = Agent.Humanoid with
{
    Quality = AvoidanceQuality.Low,
};
Console.WriteLine(options.Quality);
```

This prints `Low`. Changing the preset also changes the look-ahead time, so it isn’t an isolated neighbor-count change.

## Related recipes

- [Save and restore a crowd simulation](https://recursiveai.org/onepath/cookbook/crowd-rollback/)
- [Build a navmesh from polygon obstacles](https://recursiveai.org/onepath/cookbook/build-navmesh-from-polygon-obstacles/)


## The short answer

Set `OrcaAgent.MaxNeighbors` to cap how many other agents the standalone ORCA solver considers per step. Define `NeighborDistance` as well: the solver takes the nearest agents in range, up to the cap. For a navmesh `Crowd` in OnePath 1.2.0, set `AgentOptions.Quality` instead; there’s no independent public integer cap for that API.

## What’s the difference between neighbor count and neighbor distance?

The count is a maximum, not a target. The distance is a center-to-center cutoff, in the same units as agent positions and radii. A cap of four with only two agents in range gives the solver two agents, not four. If more than four are in range, the nearest four are selected, with ties broken by agent index. Increasing the search distance won’t make a fifth agent count if the cap is still four and four closer agents are already in range.

Each selected neighbor can constrain the walker’s velocity. Direction matters as well as distance: in the example the side neighbors are closer, but the one in front is the one that changes the walker’s forward movement. Start with a layout that represents your densest crossing or queue. Keep positions, search distance, radii, and look-ahead the same while changing the cap. Check how agents move over multiple steps in your scene; the example’s result is one step, not a collision-free guarantee or a universal cap recommendation. The [ORCA solver docs](https://recursiveai.org/onepath/docs/orca/) explain how neighboring agents become velocity constraints.

## Can you set the neighbor cap on a navmesh crowd?

Choose `Low`, `Medium`, or `High` in `AgentOptions.Quality`. The [crowd docs](https://recursiveai.org/onepath/docs/crowds/) give the corresponding caps of 4, 8, and 12, and look-ahead times of 1.5, 2, and 2.5 seconds. The navmesh crowd derives the search distance as the larger of the separation distance and `2 × radius + max speed × look-ahead time`. If you switch from `Medium` to `Low`, both the number of neighbors and the distance at which an agent can start taking them into account can change.

Use the preset if you’re already following paths on a mesh. Use the standalone solver when you need direct control of count and range: it adjusts the preferred velocity you supply, rather than finding a path to a goal. Setting `MaxNeighbors = 0` there is not the same as choosing `AvoidanceQuality.Off` on a navmesh crowd. The standalone setting only skips agent constraints; the navmesh `Off` preset skips the avoidance phase.
