Flocking seeing issue

I am trying to develop a flocking algorithm but have stumped upon a technical issue. What I basicly do is when user activates a targetpoint a fix number of Gameobjects should start flying there.

What I do is have a Flockcontroller that takes the X amount of Gameobjects and tell them to start flocking behaviour. I appoint the one closes to the target as the leader. They will all get the list of flocking-objects so they can easily determine neighbours(without looking at ALL objects in the game).

So how do I go about doing the neighbour search(used when calculating cohesion and alignment)?

  1. My first point was to have a spherecollider around all objects. I would then see if there is a Gameobjects inside, and reference that to the Flocking-list provided by the Flocking-controller. How do I determine if other objects are inside, is the only way to do OnTriggerEnter, add that to a list and then remove if OnTriggerExit, or can I (IsInsideMyCollider)?

  2. My second option would be to raycast towards all possible neighbours. I would then measure the distance, and if they are withing Viewing-radius they would count as neighbours. Is this better?

The same method will also be applied for the separation part of the algorithm


One thing I also have trouble with is separation. I want the Gameobjects themselves to control as much as possible. Here I can not allow them to be proccessed one by one, because if the first Object moves, that would make the seconds objects calculation of the current Flockposition/Flockvelocity wrong. Instead I send a list of the necessary data to all Flocking-objects as it was in the beginning of the frame and then calculate as respect to this. Is this the way to go?

Ok I think that will have to do, thanks for reading!

There is a video of Joachim Ante demoing flocking behaviour and various things you can do to increase performance.
http://unity3d.com/support/resources/unite-presentations/performance-optimization

Very good optimization explanation. Going to try to turn of flocking and see what results I get. Just for fun. The talk basicly said use triggers :smile: as I wanted in 1. So I will go for that solution and see if it works good. Thanks.

Edit: Also found the LateUpdate function to be very usefull, will be called after all other Updates are called so no worrying about some velocities being changed and others not.

Just have to figure out a way to include a Leader(random flock of birds are one thing, going towards a target is another), and some simple obstacle avoidance(would probably be best for ontrigger as well).

Also I will have a lot of different flocks so only one static UpdateCachedPositions won’t suffice as in the presentation.