In this section, we will begin looking at the simulation of a group of agents in which they are treated
less as individuals and more on mass. Crowd behavior differs from individual behavior in that properties
within the crowd (such as emotion, collision, direction and speed) propagate from one person to the next
giving the entire group its own identity. To simulate every single individual in such a group and then
produce a crowd would be labor intensive
and in fact many of the things you might consider important decision-makers for individual movement
in a crowd can be boiled down to simple vector mathematics.
There are numerous methods researchers have investigated for simulating crowds, including having people
move like particles in a fluid to using real world data.
A crowd doesn't move as a chaotic set of individuals but rather in a quasi organized fashion.
It is said to be a self-organizing system where flow lines appear in the direction of movement.
However, it has been found that if a crowd becomes denser than six people per square meter, then pockets
of turbulence will occur.
One of the most popular and simplest ways to simulate a crowd is with Reynolds flocking algorithm that
has been used in motion pictures to simulate animal herds and armies.
It's based on 3 simple rules. Turn towards the average heading of the group, turn towards the average
center of the group and avoid those around you.
We will be using these rules in combination with navmeshes and recasting to create crowds and flocks
in this section.
If you'd like to explore some simple crowd dynamics, you can do so using a navmesh.
Open up Unity and create a ground plane.
Also create 2 walls.
So I've just made the ground plane out of a normal 3D plane and I've created two walls out of some cubes
and the walls are positioned such that there's a corridor down the middle.
The blue capsule that I've added over here is going to have to walk up here, down the corridor and
over to this cube.
This capsule here is going to have to walk up and around to there. So create 2 capsules so they're just in the
Hierarchy. '3D Object > Capsule' and I've colored them differently
and then I've named one RedAgent and one BlueAgent.
I've also added in some goals. I've added a cube called BlueGoal and that cube is at the opposite
end of the path that we're going to create for the BlueAgent...and RedGoal is at the opposite end for
the RedAgent.
Now RedAgent needs a navmesh added to it.
These are just all the default settings.
BlueAgent also has a navmesh added to it
and then on both of these, I've got a script called 'AIController' and in that script
you'll find this.
First of all, I've an exposed variable (public variable) called 'goal' and that's what we will give the
agents, the 2 goal cubes, so that they know where they're heading towards.
After that we're grabbing hold of the NavMeshAgent that's attached to our character.
Don't forget to include 'using UnityEngine.AI;' library at the top. In the Start, we
then grab the agent attached to the capsule and we could also at this point, set the
destination goal which will be 'goal.transform.position' and that's all we need to do.
We can even get rid of the Update because we won't need it. Save that and attach that code to
RedAgent and BlueAgent.
Then you want to select the plane and the two cubes that are the walls and ensure that they are set to
Navigation Static and you can do that in the Navigation > Object tab
by selecting this checkbox here or you can also set them in the Inspector as we have before.
Once that's all set up, you want to go to Bake and then click on the [Bake] button and that will give you
a blue area
like you're seeing here.
So the only parts that aren't walkable are where the walls are.
This will force the agents to have to go from one end to the other.
Now to make sure that happens, select BlueAgent and then in the Inspector, on the code for the AIController,
you want to use the cube that's over here.
Sorry,
next to RedAgent as BlueAgent's goal.
So grab that cube (which I've called BlueGoal) and then you would drag-and-drop that into the
goal slot for the blue capsule. For the red capsule,
it's the other way around.
The goal will be the cube at the other end.
Now when you play this, the agents will walk the path and swap over from each other.
Now that was sped up by the magic of video editing
but if you want your agents to move faster, don't forget that you can select them in the Hierarchy, go over
to the Inspector and change their Speed value.
Mine are currently set to 10, which is quite fast by itself.
When we start adding more agents, you might want it to run a little bit slower.
So, first of all, let's have a look at that phenomenon of 'self-organizing by flow', which is where 2 groups
coming together, moving in opposite directions, will form lines of flow through the crowd.
All we need to do is select both of our agents and then Ctrl-D and create a whole heap of copies.
Let's create 20 copies. Now with those in place, let's press Play and we can watch them.
They're all in the same spot, so they're going to explode from the same position that they're in. Now
as they come together,
they will form lines of flow
and that's only a small group, so you get 2 clear lines of flow where they overlap.
From here, you could select all of those duplicates and duplicate them twice again to get some more
and then press Play. Again,
they explode out from the single place and now they're going to crowd in this area here and we will
find that they will form lines of flow.
You get a little bit of turbulence, as you saw there, where it got quite dense and some guys back here actually
got stuck in that crowd as well.
You can modify the simulation as much as you like, by adding more walls and more agents.
Here can see I've got 4 different colored agents and even when they crowd you can clearly see the flow
lines forming.
In A.I., when this type of behavior occurs in a system that's not specifically programmed to do this, it's called
emergent behavior and it's these clever lifelike results
we're trying to get in our NPCs with as minimum programming effort as possible. In the next lecture we
will work on a crowd simulation with animated human characters.