AI Advanced Flocking

In our thriller/horror game "Stilla Natt" we wanted to give some life to the otherwise pretty static environment of the game. The result is rats with a more advanced flock/flee behavior. They interact with each other, the player, and the Lucia (monster actor). This is all done with the Finite State Machine.

A snippet from the align algorithm.

A snippet from the align algorithm.

BOID behavior

First, I implemented a BOID system with cohesion, alignment, and separation. Right now, the AI doesn't care about the navmesh, it only uses the BOID algorithms and PhysX collisions for navigation. They also don't care about the player right now, only each other.

Engaging the player

The game gets a little more dynamic when the AI cares about the player. Who doesn't love getting swarmed by cube-like rats?

Rat-like behavior

To give the rats a little more realism, I added some idle behavior. The rats now uses the navmesh to find a position within a determined range where it can pathfind to. They still use the BOID behavior, but more weight is added to their individual movement.

Here is a snippet showing the rat roam state.

Interacting with the monster

One major feature of the rats are that they can tell the player where the monster is by looking at their flee pattern. When a rat flees, it tries to find a waypoint that is in the opposite direction of the monster. When the location of a valid waypoint it can pathfind to is found, it starts pathfinding toward it. To get the rats to behave as a unit, the fleeing rat tells all nearby rats to flee to the same waypoint.

Part of the code finding a waypoint closest to a specific direction.

Better grouping

To make the rats feel more alive I decided to implement control groups. Rats in the same control group will try to flee to the same waypoint. Two control groups can't chose the same waypoint (if there are other waypoints available). This results in a more dynamic fleeing behavior.