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 to follow.
The code in the Flock.cs
file is as follows:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class Flock : MonoBehaviour { internal FlockController controller; void Update () { if (controller) { Vector3 relativePos = steer() * Time.deltaTime; if (relativePos != Vector3.zero) rigidbody.velocity = relativePos; // enforce minimum and maximum speeds for the boids float...