Give each enemy a push away from every neighbor closer than its personal space, and when that push points against its walking direction, stop walking until the way clears. In our Godot 4.7 test, 30 enemies that only chased the player ended up in 107 overlapping pairs, and the closest two were in the same spot. With the script there were 0 overlapping pairs and the crowd stood still.
The code
This is for enemies that do not collide with each other, which is common in hordes so they do not block each other in corridors. Every physics frame, call steer(position, player_position, other_positions) for each enemy and use the result as its velocity, for example as velocity before move_and_slide().
extends Node
## Keep a horde from merging into one blob while it chases the player.
## For enemies that do not collide with each other: call steer() for each one every
## physics frame and move it by the result (velocity, meters per second).
@export var speed := 3.0
@export var personal_space := 1.0 # meters between enemy centers
@export var stop_distance := 1.2 # stop walking this far from the player
func steer(me: Vector3, target: Vector3, others: Array) -> Vector3:
var to_target := target - me
to_target.y = 0.0
var v := Vector3.ZERO
if to_target.length() > stop_distance:
v = to_target.normalized() * speed
var push := Vector3.ZERO
for o in others:
var d: Vector3 = me - o
d.y = 0.0
var dist := d.length()
if dist > 0.0001 and dist < personal_space:
push += d / dist * (personal_space - dist) / personal_space # stronger the closer they are
if push.length() > 0.0001:
var away := push.normalized()
var into := v.dot(away)
if into < 0.0:
v = Vector3.ZERO # someone is in the way: wait behind them instead of pushing in
return (v + push * speed * 2.0).limit_length(speed)
What we measured
30 enemies started at random spots 10 to 14 m from the player, moving 3 m per second and stopping 1.2 m from the player. We moved them in steps of 1/60 of a second for 10 seconds, without physics, and counted pairs closer than 0.8 m, the width of one enemy. Two runs gave the same numbers.
- Chasing only: 107 overlapping pairs. The closest pair was 0.00 m apart. All 30 stood between 1.15 and 1.2 m from the player.
- The script: 0 overlapping pairs, the closest pair 0.95 m apart. The crowd spread out to 4.32 m from the player, and in the last second the average speed was 0.05 m per second, so they stood still instead of shuffling.
- The neighbors pushed one enemy in to 0.44 m from the player.
Why it happens
Every enemy walks straight at the same point, so they all end up on the same small circle around the player. A 1.2 m circle is about 7.5 m around, and 30 enemies 0.8 m wide need 24 m. Waiting when someone is in the way lets the late ones stand behind the first row instead of walking into it.
What to change
personal_space: distance between enemy centers. Set it a bit wider than your enemies.stop_distance: how close they walk to the player.speed: walking speed. The push never moves an enemy faster than this.
To keep the player clear too, add the player's position to others.
Checking every enemy against every other is fine for a few dozen. For hundreds, only pass the neighbors from a grid or an Area3D.
Where the horde comes from: spawn enemies where the camera cannot see them.
Effects for the horde: Zombie Survival VFX for Godot 4 has Zombie Rise from the ground, Zombie Death in smoke or toxic, Infection Fog, Molotov and Road Flare.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Zombie Survival VFX for Godot 4 has Zombie Rise from the ground, Zombie Death in smoke or toxic, Infection Fog, Spore Cloud, Molotov, Road Flare and a campfire Safe Zone.
More how-tos for Zombie Survival VFX for Godot 4: A fire or poison zone that keeps hurting while you stand in it
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene