# Understanding Steering Behaviors: Leader Following

This post is part of a series called Understanding Steering Behaviors.
Understanding Steering Behaviors: Path Following
Understanding Steering Behaviors: Queue

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:

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:

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:

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:

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:

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:

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:

Any character will immediately evade its current position if it's in the leader's sight:

Tip: the leader following force is a combination of several forces. When a character is influenced by the following force, it's actually being influenced by the arrive, the separation and the evade force, all at the same time.

## 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.

## 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!