O'Reilly logo

Unity 4.x Game AI Programming by Thet Naing Swe, Aung Sithu Kyaw, Clifford Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Alternative implementation

Here's a simpler implementation of the flocking algorithm. In this example, we'll create a cube object and place a rigid body on our boids. With Unity's rigid body physics, we can simplify the translation and steering behavior of our boid. To prevent our boids from overlapping each other, we'll add a sphere collider physics component.

We'll have two components in this implementation as well: individual boid behavior and controller behavior. The controller will be the object that the rest of the boids try and follow.

The code in the Flock.cs file can be shown as follows:

using UnityEngine; using System.Collections; using System.Collections.Generic; public class Flock : MonoBehaviour { internal FlockController controller; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required