Alternative implementation

In this section, we will use the Unity physics engine to simplify the code a bit. In fact, in this example, we will attach a rigidbody component to our boids so that we can use rigidbody properties for the translation and steering behaviors of our boid. The rigidbody component is also useful to prevent our boids from overlapping with each other.

In this implementation, we have two components: the individual boid behavior and the controller behavior (the element that we referred to as the flock controller in the previous section). As before, the controller will be the object that the rest of the boids follow.

The code in the Flock.cs file is as follows:

using UnityEngine; using System.Collections; using System.Collections.Generic; ...

Get Unity Artificial Intelligence Programming - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.