Squad Coordination
Five individually smart enemies don't make a smart squad. Left alone, they all pick the same best cover point, all charge at once, all shout the same line and all reload together. Squad AI adds a thin layer above the individuals that hands out roles, positions and permission to act, while each member still runs its own behavior tree or state machine.
There are two classic ways to organise it:
| Style | How it works | Example uses |
|---|---|---|
| Centralised (a squad "manager" or commander) | one object looks at the whole squad and the threat, then writes orders into each member's blackboard | flanking manoeuvres, formations, assigning cover points |
| Emergent (shared data, local rules) | members read and write shared state (claimed cover, attack tokens, last-known enemy position) and decide for themselves | "don't all attack at once", spreading out, sharing sightings |
Most shipped games mix both: a light manager for big decisions and shared data for the rest.
Formations: slots relative to the leader
A formation is a list of slot offsets in the leader's local space. Each update the offsets are rotated by the leader's heading and added to its position; members then arrive at their slot. Because the offsets are local, the formation turns with the leader:
using System;
var slots = new (string Name, V2 Offset)[] // x = forward, y = left, in the leader's frame
{
("left wing", new V2(-1.5, 1.5)),
("right wing", new V2(-1.5, -1.5)),
("rear", new V2(-3, 0)),
};
foreach (var (pos, headingDeg) in new[] { (new V2(0, 0), 0.0), (new V2(10, 0), 90.0), (new V2(10, 8), 180.0) })
{
Console.WriteLine(quot;leader at {pos} heading {headingDeg,3:F0} deg");
double a = headingDeg * Math.PI / 180;
V2 forward = new(Math.Cos(a), Math.Sin(a)), left = new(-Math.Sin(a), Math.Cos(a));
foreach (var (name, o) in slots)
Console.WriteLine(quot; {name,-10} slot at {pos + forward * o.X + left * o.Y}");
}
readonly record struct V2(double X, double Y)
{
public static V2 operator +(V2 a, V2 b) => new(a.X + b.X, a.Y + b.Y);
public static V2 operator *(V2 a, double k) => new(a.X * k, a.Y * k);
static string F(double v) => (Math.Round(v, 2) + 0.0).ToString("F2");
public override string ToString() => quot;({F(X)}, {F(Y)})";
}leader at (0.00, 0.00) heading 0 deg
left wing slot at (-1.50, 1.50)
right wing slot at (-1.50, -1.50)
rear slot at (-3.00, 0.00)
leader at (10.00, 0.00) heading 90 deg
left wing slot at (8.50, -1.50)
right wing slot at (11.50, -1.50)
rear slot at (10.00, -3.00)
leader at (10.00, 8.00) heading 180 deg
left wing slot at (11.50, 6.50)
right wing slot at (11.50, 9.50)
rear slot at (13.00, 8.00)
In practice, assign slots by distance (each member takes the nearest free slot) rather than by index, or members will cross paths every time the formation turns. And project slots onto the navmesh: a slot inside a wall is common when the leader walks along one.
Attack tokens: don't all shoot at once
Players hate being swarmed and love being challenged. The standard trick, used in many action games, is a small pool of attack tokens per target. An enemy must hold a token to attack; without one it circles, takes cover, taunts or repositions. When the attack ends, the token goes back after a short cooldown. The number of tokens becomes a difficulty knob: 1 on easy, 3 on hard.
using System;
using System.Collections.Generic;
using System.Linq;
var enemies = new List<Enemy>
{
new("Grunt1", 4.0), new("Grunt2", 2.5), new("Archer", 9.0), new("Brute", 3.0), new("Grunt3", 6.0),
};
var pool = new TokenPool(tokens: 2);
for (int tick = 0; tick < 12; tick++)
{
pool.Tick();
// Members ask in order of priority: closer is better, and every tick spent waiting counts like 2 m.
foreach (var e in enemies.OrderBy(e => e.Distance - 2 * e.Waited))
{
if (e.AttackLeft > 0) { e.AttackLeft--; if (e.AttackLeft == 0) pool.Release(e.Name); continue; }
if (pool.TryTake(e.Name)) { e.AttackLeft = 2; e.Waited = 0; } // an attack lasts 2 ticks
else e.Waited++;
}
var attacking = enemies.Where(e => e.AttackLeft > 0).Select(e => e.Name);
var waiting = enemies.Where(e => e.AttackLeft == 0).Select(e => e.Name);
Console.WriteLine(quot;tick {tick}: attacking [{string.Join(", ", attacking)}] circling [{string.Join(", ", waiting)}]");
}
class Enemy(string name, double distance)
{
public string Name = name;
public double Distance = distance;
public int AttackLeft, Waited;
}
class TokenPool(int tokens)
{
readonly Dictionary<string, int> cooldown = new(); // released tokens come back after 1 tick
readonly HashSet<string> holders = new();
public bool TryTake(string who)
{
if (holders.Count + cooldown.Count >= tokens) return false;
holders.Add(who);
return true;
}
public void Release(string who) { holders.Remove(who); cooldown[who] = 1; }
public void Tick()
{
foreach (var k in cooldown.Keys.ToList())
if (--cooldown[k] < 0) cooldown.Remove(k);
}
}tick 0: attacking [Grunt2, Brute] circling [Grunt1, Archer, Grunt3]
tick 1: attacking [Grunt2, Brute] circling [Grunt1, Archer, Grunt3]
tick 2: attacking [] circling [Grunt1, Grunt2, Archer, Brute, Grunt3]
tick 3: attacking [] circling [Grunt1, Grunt2, Archer, Brute, Grunt3]
tick 4: attacking [Grunt1, Grunt3] circling [Grunt2, Archer, Brute]
tick 5: attacking [Grunt1, Grunt3] circling [Grunt2, Archer, Brute]
tick 6: attacking [] circling [Grunt1, Grunt2, Archer, Brute, Grunt3]
tick 7: attacking [] circling [Grunt1, Grunt2, Archer, Brute, Grunt3]
tick 8: attacking [Grunt2, Archer] circling [Grunt1, Brute, Grunt3]
tick 9: attacking [Grunt2, Archer] circling [Grunt1, Brute, Grunt3]
tick 10: attacking [] circling [Grunt1, Grunt2, Archer, Brute, Grunt3]
tick 11: attacking [] circling [Grunt1, Grunt2, Archer, Brute, Grunt3]
Never more than two enemies attack at a time, and there are gaps between attack waves (the cooldown) that give the player room to breathe. The priority matters as much as the pool. With distance alone, Grunt2 and Brute (the two closest) would win every token for ever; the others would circle all fight long. Here every tick of waiting is worth 2 m of distance, so the attacks rotate: the two closest first, then Grunt1 and Grunt3, then the archer finally gets a turn alongside Grunt2. Many games also give ranged attackers a separate pool.
Tip: Make token holders visible: the attacking enemy raises its weapon, steps forward, growls. Players read the pattern without knowing it exists and feel like skilled duellists instead of victims.
Other coordination patterns
- Shared sightings: the first member who perceives the player writes the position and time into the squad blackboard; the others react to that record (see Memory & Awareness). Add a short delay and a radio voice line so the player can tell why everyone knows.
- Flanking: the manager picks one member to suppress from the front and sends another along a path that avoids the player's line of sight (a path cost from a visibility influence map).
- Claimed cover: members reserve cover points so nobody shares a crate.
- Bark coordination: only one member speaks a given line within a few seconds; the others answer ("Moving!" / "Covering!"). Cheap, and it sells the whole squad.
Test yourself
Why are formation slots stored as offsets in the leader's local space?
- They use less memory
- Rotating them by the leader's heading makes the whole formation turn with the leader
- Engines require local space
- So members can ignore the leader
What problem do attack tokens solve?
- Pathfinding cost
- Every enemy attacking at the same moment, which feels unfair
- Animation blending
- Saving the game
In the token program, why does the archer (the farthest enemy) eventually get to attack?
- Archers have their own pool
- Each tick of waiting lowers its priority value by 2 m, so it eventually outranks closer enemies
- The pool grows over time
- Closer enemies run out of health