With this project I took my time to learn how to do an simple flocking simulation. My knowledge of this is still elementary, but I'm planning on implementing this as an game mechanic in one of my up coming projects. For now...look at the little fishies.
This is done by each entity (Craig Reynolds calls them boids) calculating the other entities position and creating an average center position Getting the other's entities positions -
centerVector += neighbor.transform.position;
After dividing the sum of the positions by the size of the flock -
centerVector /= flockSize;
This is done by adding all the Forward Vectors (facing directions) and dividing them by the size of the flock. Since the entities are just going forward I used the same centerVector
variable to calculate the foward direction:\
var desiredFacingDirection = (centerVector + avoidanceVector) - transform.position;
When another entity is too close to one another, you'll need to calculate a new forward direction by using the flock forward heading (vector) +
the direction of the where to avoid the other entity + the flocks position.
Getting the avoidance direction vector: \
if (closetNeighbor < _manager.NeighborDist) avoidanceVector += transform.position - neighbor.transform.position;