Flocking / Boids


Flocking Algorithm: Boids

This analgorithm, originally created by Craig Reynolds in 1986, simulates natural flocking patterns seen in nature (birds, fish, etc). The algorithm consists of three main 'steering behaviors': cohesion, alignment, and separation.

Separation is the behavior of agents avoiding each other. This requirment is only needed in simulations - objects in real life cannot travel through each other.

Cohesion is the behavior of agents attempting to adjust their position in the flock toward the average central location. This gives the effect of agents prefering large flocks instead of lone travel.

Alignment is the behavior of agents attempting to travel in the same average heading. This gives the effect of the agents progressing to a common goal.

I added object avoidance by using very simple ray casting from each agent, therefore objects in the scene can be dynamic and the flocks will avoid them - further simulating natural flocking. I created this 3D simulation by following Craig Reynolds paper as well as referencing a useful 2D tutorial.

Source Code
Agent Prefab

Using prefabs allowed me to create one generic agent, and then easily duplicate it to create large groups.

This made annimating much simpler. I only had to implement animations for one object, then add an offset to each duplicated agent so that the animation would look random.

Behaviors

I created separate scripts for each behavior and then combine them in one composite script. This allowed me to add modifiers to each which could change the impact of each of the behaviors.

For example, I found that increasing the cohesion multiplier too much caused the flocks to look more robotic.

More about the project

I undertook this project to learn how to use Unity. Following this algorithm allowed me to learn scripting, prefabs, animations, 3D physics, materials, lighting, camera locations, vectors, asset utilization, and a basic understanding of Unity.

To the right are images of Craig Reynold's Paper and Board To Bits Games' Tutorial. These were both great resources to follow. I also recommend Brackey's Unity tutorials on animation and prefabs in Unity.


Tools Used

  • Languages:
    • C#
  • Technologies:
    • VS Code
    • GitHub
    • Unity
  • Tools:
    • prefabs
    • filters
    • annimation
    • colliders