Boid Flocking and Pathfinding in Unity

June 20th, 2012 by Anthony Capobianchi

The quest for creating believable, seemingly intelligent movement among groups of characters is a challenge many game developers encounter. A while back, I had an idea for an app that required units of moveable objects to be able to coordinate and get from point ‘A’ to point ‘B’., I’ve never built anything like an RTS before, so this concept was something new to me. I searched forums and articles looking for the answers on how people achieve this sort of behavior. The majority of the help I could find was in a system referred to as “Boid” or “Flocking”, A Boid system can be set to simulate flocks or herds, allowing moving units to animate independently, giving the illusion of artificial intelligence. Over the next three blog posts, I will outline solutions to the following problems:

  1. Boid System – Creating a system to keep objects together in a coherent way.
  2. Radar Class – Creating a class that will detect if any Boids are within a certain distance from another Boid.
  3. Path and Obstacle Avoidance – Getting an object to follow a path as well as avoid obstacles that keep them from getting to their destination.
  4. Ray Caster – Setting up a ray caster that will be used to place a destination object in the scene for the Boids.
  5. Destination Points – Organizing a number of different destination points for the Boids that will prevent   them from piling on top of each other.


Normally, I would split up the functions into different scripts depending on their function, for instance, a script for calculating the Boid behavior force, a script for the radar, and a script for  path calculating. However, to keep the count down and to avoid possible confusion of not always knowing which script a function goes into, I consolidated it into only a few scripts -

I.    Boid.cs
II.    Destination.cs
III.    DestinationManager.cs

NOTE: All properties and variables should go at the top of your classes, in my code examples I am putting the properties above the methods to show you which properties you need in your script and why.

•    Boids

The Boid system is accomplished by creating a script (Which was named Boid.cs) that controls the behaviors of their basic movement. This included coherency, which is the will to stick together, and also separation, which is the will to keep apart. If you are unfamiliar with the concept of Boids or flocking, a great article about it can be found at and a C# Unity example can be found here:

To set up my Boid.cs script, I set up these properties:

My Boid behaviors are set up like this:


We need a way for every Boid to know if there are other Boid objects surrounding it within a certain radius. In order to create this effect, we will create functions that will handle radar scans and what the scanner is looking for. The radar will be called to scan a few times every second. It is not using the Update function to get called. If it was, every frame would be making a collision check using Physics.OverlapSphere. This could cause frame rates to drop, especially if you have a lot of Boids in the scene.

In my Boid.cs script my Radar functions are set up like this:

In my next post, I will explain how I solved the problem of getting an object to follow a path while avoiding obstacles.In addition, I will explain what will be needed to apply the forces that are calculated by the Boid and pathfinding systems to get our characters moving.
Anthony Capobianchi

, , , ,

One Response to “Boid Flocking and Pathfinding in Unity”

  1. Infrared5 Blog » Blog Archive » Boid Flocking and Pathfinding in Unity, Part 2 Says:

    [...] Read part one here. [...]

Leave a Reply