This is an implementation of flocking behavior in Unity. Inspiration was drawn heavily from The Nature of Code by Daniel Shiffman.
Bin-lattice spatial subdivision is used to improve performance; my computer can easily handle flocks of ~200 boids (bird-thingies) at 200 FPS. The
FlockingBehavior has numerous properties to drive boid motivations and behavior.
Below is an example of a flock with loose steering and no boid colliders; it reminds a bit of a flock of starlings:
Another example with tighter steering and boid colliders:
Gizmos on the flock include a grid illustrating the bin-lattice spatial subdivision, which can be helpful when tweaking the flock settings.
- Add paths for boids to follow when the target is out of sight.
- Add option to check adjacent bins/cells when boids check for neighbors.
- Would improve flock appearance (reduce lines when clumping), but may negatively impact performance.
Hopefully someone out there on the interwebs can use this in a game they’re developing, especially since I can never seem to finish one 😅
If you do, I’d love to hear about it!