← Pixel Dreamer Studio

Keep a chasing horde from stacking into one enemy

Push each enemy away from neighbors that are too close, and stop it walking forward when someone is in the way.

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.

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

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