Alternative implementation
In this section, we use the Unity physics engine to simplify the code a bit. In fact, in this example, we attach a Rigidbody
component to the boids to use the Rigidbody
properties to translate and steer them. In addition, the Rigidbody
component is also helpful in preventing the other boids from overlapping with each other.
In this implementation, we have two components: the individual boid behavior and the controller behavior (the element referred to as the flock controller in the previous section). As before, the controller is 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; public class Flock : MonoBehaviour { Â Â Â Â internal FlockController controller; Â Â Â Â private new Rigidbody rigidbody; Â Â Â Â private void Start() { Â Â Â Â Â Â Â Â rigidbody...