How to get NavAgent2D to move side by side?

Godot Version



I have a setup where my enemies who are CharacterBody2D with NavigationAgent2D are moving on a NavMesh from one point to the other. I have Avoidance enabled and it’s working fine. The enemies are created side by side but they move behind each other in a file. I would like them to move side by side to form a horde or a swarm. Also, they get stuck at choke points.

nav_agent.path_desired_distance = 4
nav_agent.target_desired_distance = 4

Here is a video


i tried it myself and got pretty similar result, it looks like you will need to control the speed of the agent when collided with each other (example increase speed or change direction ) or add deceleration during turning around.

I will copy my reddit answer here. These features are not “simple” to make, there is a reason why so many “professional” games with often times millions for budget fail at them regularly.

The features that you are looking for, formation movement and path planning, is not something the navigation system can provide. They are hard if not impossible to provide in a useful and generic enough way that fits many projects, and are best implemented specific to a project.

For basics of formation movement define a group leader agent that is the base for the formation and its orientation, then make the other agents move to their assigned formation offset positions. Now how to deal with narrow or occupied space is another rather difficult problem to solve.

Path planning more commonly is found with grid layouts, because it is far easier to implemented with grid cells than with arbitrary shaped navigation meshes. The entire reason devs use (or should use) navigation meshes are because they are more efficient to cover large, arbitrary surfaces compared to grids and allow for more detailed surface movement compared to just point navigation.

Now for path planning you need to have small cells to control what is already “occupied” or needs a cost increase so the agents will take different paths. At that point the strength of the navigation mesh works against it as you can not define that in a single large polygon that covers the entire surface. So most path planning solutions fall back to a performance costly grid layout or start to slice their polygons into smaller polygons.