Using the Reynolds algorithm

Without further ado, let's dive into the Reynolds flocking algorithm. There are two main scripts for our flocking implementation: Boid.cs and FlockController.cs. The sample code for this chapter provides a scene with all the necessary setup for testing. You'll also notice a third script named TargetMovement.cs, which we use to move a target that our flock will follow around the scene.

For our boid, we can use a simple cube as a prefab. Of course, feel free to replace the cube with any art you want. Let's add the Boid.cs script to our boid prefab. The code looks like this:

using UnityEngine;public class Boid : MonoBehaviour {    [SerializeField]    private FlockController flockController;     //The modified direction for ...

Get Unity 2017 Game AI Programming - Third 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.