Set a custom cap in ORCA, OnePath’s local collision-avoidance solver, or use a navmesh crowd preset.
dotnet add package RecursiveAI.OnePathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OnePath.Crowds.Orca; var walker = new OrcaAgent(); walker.MaxNeighbors = 4; Console.WriteLine(walker.MaxNeighbors);
Unzip the example, then dotnet run.
Prints:
4The full program
RecursiveAI.OnePath brings in RecursiveAI.OneMath for Vector2D; no separate install is needed.
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}"); }
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.
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
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 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 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.