Group and Individual Behaviors Simultaneously?

I'd like to have a herd of animals that move together, but also have individual needs/behaviors.

A quick example would be a player trying to herd some cows from enclosure A to B. During this walk the cows "flock" together, but some cows see food, the cows that are farther from the player should try to get the food, while cows closer to the player have a greater need to obey so they continue to enclosure B.

I'm thinking that in this example, I could use one global flocking behavior anytime a cow has to move and has a neighbor.... so here in your flocking script:
        // Don't move if there are no neighbors
        if (neighborCount == 0)
            //use individual behavior tree here for movement?

That handles the problem of a herd of 1. But there are also times where a cow has neighbors but also breaks from the "group think" and becomes an individual and I'm not sure how to make that transition.

He can't be removed from the group flock behavior entirely when he's an individual, the other cows should still treat him like he's in the herd because he might be standing in the middle of the herd, but his goals now serve the individual instead of the group.

Or would it somehow be better to go the other way and only have individual behavior trees on all the cows and try to sneak in some alignment, cohesion, and separation rules?



Staff member
For this situation can you remove the agent from the flock array and then start a new behavior tree that has the individual logic? The other flocking agents should still avoid the individual agent if you mark the agent as an obstacle for the NavMeshAgent.
Okay, so as I'm working on this, I think I've got a new/better solution:

- I'm going to tweak the flocking math to create some more stylized grouping behaviors to create a large herd that includes all cows.
- This 'master' herd will only control movement, and only for cows in the 'active list'.
- Each cow will also have it's own behavior tree running sensors (seeing, hearing, smelling) while the 'master' herd controls it's movement.
- When a cow senses something to interact with, it will remove itself from the master herd's movement list and control it's own movement.
- When it no longer senses anything in the world, it will add itself back to the master herd's movement list and only check it's sensors. No individual movement.

Do you see any issues with this? I feel that this is simpler because the herd behavior only controls movement and doesn't have to know or care about why cows are removed/added to the herd. This will allow for all the 'complex' behaviors to be added in the cow's individual behavior tree.


Staff member
That is a good approach, and is actually something similar to the approach used by the Formations and Tactical Packs. With those assets there is a follow orders task that will give control over to the main behavior tree. When the following agent wants to leave the formation they can then just stop that follow orders task.