This demonstration is for ‘Flocking ’ behaviour, where I have fifty vehicle(or actors) moving around flocking over the map. I have implemented three major flocking behaviors, ‘Alignment,Separation and Cohesion’. I have provided sliders for all three of them, so you can test each behaviour individually and look at the result. I have made this is Unity 5, you can find the code here at GitHub.

Demo video: 

I began with implementing screen wrap similar to one I did in Obstacle Avoidance. Them I made a Flocking Controller class and attached it to every actor in the demo. Here are the snippets:


For every actor, I calculate the distance between itself and other 49 actors except itself. Then I decide if the distance is less than a predefined neighbor distance, the actors are neighbors. After that the total number of neighbors is calculated, and a vector is divided accordingly. Then the vector is normalized, multiplied by max speed and returned.



I calculated the neighbors in similar way as above, then I subtract the positions of the actor with its neighbors to they come together to simulate cohesion.



I calculate the neighbors, then I calculate steering vector by subtracting the neighbor’s position with the actor’s to make them move away from each other. Then I continually add it to the separation vector for a smooth turning.


Then I use the returned vectors from each of the functions above, add it with predefined weights and apply them to the steering vector. I also reset the neighbourcount every time a function is called.



Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: