In addition to the ability to follow a path, a character (or a group of them) also needs to be able to follow a specific character (like a squad leader). This can be achieved using the leader following behavior, which I'll explain in this tutorial.
Note: Although this tutorial is written using AS3 and Flash, you should be able to use the same techniques and concepts in almost any game development environment. You must have a basic understanding of math vectors.
Introduction
The leader following behavior is a composition of other steering forces, all arranged to make a group of characters follow a specific character (the leader). A naive approach would use seek or pursuit to create a follow pattern, but the result is not good enough.
In the seek behavior a character is pushed towards a target, eventually occupying the very same location as that target. The pursuit behavior, on the other hand, pushes a characters towards another character, but aiming to catch it (based on predictions) instead of just following it.
In the leader following behavior, the objective is to stay close enough to the leader, but slightly behind it. The character should also move faster towards the leader when they are distant, but should slow down when the distance is small. This can all be achieved by combining three steering behaviors:
- Arrive: move towards the leader and gradually slow down, eventually stopping the movement.
- Evade: if the character is in the leader's way, it should quickly move away.
- Separation: avoid crowding when several characters are after the leader.
The next sections explain how each of these behaviors are combined to create the leader following pattern.
Finding the Right Spot to Follow
A character should try to stay slightly behind the leader during the following process, like an army staying behind its commander. The point to follow (named behind
) can be easily calculated based on the target's velocity, since it also represents the character direction. Check the pseudo-code:
tv = leader.velocity * -1; tv = normalize(tv) * LEADER_BEHIND_DIST; behind = leader.position + tv;
If the velocity vector is multiplied by -1
, the result is the inverse of the velocity vector. That resulting vector (called tv
) can then be normalized, scaled accordingly, and added to the character's current position.
Here is a visual representation of the process:

The greater LEADER_BEHIND_DIST
is, the greater the distance between the leader and the behind point is. Since the characters will follow that point, the more distant it is from the leader, the more distant the characters will remain from the leader.
Following and Arriving
The next step is make a character follow the leader's behind
point. Just like all other behaviors, the following process will be guided by a force, generated by the followLeader()
method:
private function followLeader(leader :Boid) :Vector3D { var tv :Vector3D = leader.velocity.clone(); var force :Vector3D = new Vector3D(); // Calculate the behind point tv.scaleBy(-1); tv.normalize(); tv.scaleBy(LEADER_BEHIND_DIST); behind = leader.position.clone().add(tv); // Creates a force to arrive at the behind point force = force.add(arrive(behind)); return force; }
The method calculates the behind
point and creates a force to arrive at that point. The followLeader
force can then be added to the character's steering force, just like all other behaviors:
steering = nothing(); // the null vector, meaning "zero force magnitude" steering = steering + followLeader(); steering = truncate (steering, max_force) steering = steering / mass velocity = truncate (velocity + steering, max_speed) position = position + velocity
The result of that implementation is a group of characters able to arrive at the leader's behind
point:
Avoiding Crowding
When characters are very close to each other while following the leader, the result might seem unnatural. Since all characters will be influenced by similar forces, they tend to move similarly, forming a "blob". This pattern can be fixed using separation, one of the rules that guide the flocking behavior.
The separation force prevents a group of characters from crowding, so they keep a certain distance from each other. The separation force can be calculated as:
private function separation() :Vector3D { var force :Vector3D = new Vector3D(); var neighborCount :int = 0; for (var i:int = 0; i < Game.instance.boids.length; i++) { var b :Boid = Game.instance.boids[i]; if (b != this && distance(b, this) <= SEPARATION_RADIUS) { force.x += b.position.x - this.position.x; force.y += b.position.y - this.position.y; neighborCount++; } } if (neighborCount != 0) { force.x /= neighborCount; force.y /= neighborCount; force.scaleBy( -1); } force.normalize(); force.scaleBy(MAX_SEPARATION); return force; }
The separation force can then be added to the followLeader
force, making it able to push the characters away from each other at the same time that they try to arrive at the leader:
private function followLeader(leader :Boid) :Vector3D { var tv :Vector3D = leader.velocity.clone(); var force :Vector3D = new Vector3D(); // Calculate the behind point tv.scaleBy(-1); tv.normalize(); tv.scaleBy(LEADER_BEHIND_DIST); behind = leader.position.clone().add(tv); // Creates a force to arrive at the behind point force = force.add(arrive(behind)); // Add separation force force = force.add(separation()); return force; }
The result is a much more natural following pattern:
Staying Out of the Way
If the leader suddenly changes the current direction, there's a chance that the characters will end up getting in the way of the leader. Since the characters are following the leader, it makes no sense to allow them to stay in front of the leader.
If any character gets in the way of the leader, it should immediately move away to clear the route. This can be achieved using the evade behavior:

In order to check whether a character is in the leader's sight, we use a similar concept to how we detect obstacles in the collision avoidance behavior: based on the leader's current velocity and direction, we project a point in front of it (called ahead
); if the distance between the leader's ahead
point and the character is less than 30
, for instance, then the character is in the leader's sight and should move.
The ahead
point is calculated exactly like the behind
point, with the difference that the velocity vector is not inverted:
tv = leader.velocity; tv = normalize(tv) * LEADER_BEHIND_DIST; ahead = leader.position + tv;
The followLeader()
method must be updated to check if the character is on the leader's sight. If that happens, the variable force
(representing the following force) is added by the return of evade(leader)
, which returns a force to evade the leader's location:
private function followLeader(leader :Boid) :Vector3D { var tv :Vector3D = leader.velocity.clone(); var force :Vector3D = new Vector3D(); // Calculate the ahead point tv.normalize(); tv.scaleBy(LEADER_BEHIND_DIST); ahead = leader.position.clone().add(tv); // Calculate the behind point tv.scaleBy(-1); behind = leader.position.clone().add(tv); // If the character is on the leader's sight, add a force // to evade the route immediately. if (isOnLeaderSight(leader, ahead)) { force = force.add(evade(leader)); } // Creates a force to arrive at the behind point force = force.add(arrive(behind, 50)); // 50 is the arrive radius // Add separation force force = force.add(separation()); return force; } private function isOnLeaderSight(leader :Boid, leaderAhead :Vector3D) :Boolean { return distance(leaderAhead, this) <= LEADER_SIGHT_RADIUS || distance(leader.position, this) <= LEADER_SIGHT_RADIUS; } private function distance(a :Object, b :Object) :Number { return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }
Any character will immediately evade its current position if it's in the leader's sight:
Demo
Below is a demo showing the leader following behavior. The blue soldier (the leader) will arrive at the mouse cursor, while the green soldiers will follow the leader.
When the player clicks anywhere in the screen, all soldiers will face the mouse cursor and shoot. The monsters will arrive at random locations, every few seconds.
- Sprites: Top/Down Shoot 'Em Up Spritesheet by takomogames
- Background: Alien Breed (esque) Top-Down Tilesheet by SpicyPixel
Conclusion
The leader following behavior allows a group of characters to follow a specific target, staying slightly behind it. The characters also evade their current location if they eventually get in the way of the leader.
It's important to note that the leader following behavior is a combination of several other behaviors, such as arrive, evade and separation. It demonstrates that simple behaviors can be combined to create extremely complex movement patterns.
Thanks for reading! I hope you can start combining the previously explained behaviors to create your own complex behavior!
Subscribe below and we’ll send you a weekly email summary of all new Game Development tutorials. Never miss out on learning about the next big thing.
Update me weeklyEnvato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!
Translate this post