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 en masse.
Crowd behavior differs from individual behavior in that properties within the crowd, such as a motion,
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 metre, 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 three 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 NAV measures and rate casting to create crowds and
flocks in this section.
So to begin our exploration of crowds, we're going to create a really simple crowd here on a nav mesh
so that we can investigate the principles of flow and how there's self-organization within that.
What I'd like you to do is create a new unity project.
And inside of it, we're going to add just a simple plane like this.
Now, I've just put a texture on there so that you can see it better.
I've created two wall objects and these were just cubes that I've stretched out, so I've basically
just selected them and stretched them in different ways so that you get these two walls like that.
Now we also need a goal location for our two agents.
We've got a blue capsule and a red capsule.
These are going to be our nav mesh agents.
I need a cube added here, which will be called red goal, as you can see over in the hierarchy and
this one here, which will be the blue goal.
What we're going to do is get our agents to travel through this course to get to their particular goal.
Next, you can create the agent.
So the agents are just capsules.
And to those capsules I've added a blue material and a red material.
Now there's some other files going on down in my assets.
Don't you worry about those.
We'll get to those in the next lecture.
Just for now.
We aren't needing them.
Okay, so once you've got that particular set up working, we're going to make a mesh.
So we need to select the plane, which is the ground and the walls.
And from that, we're going to then go over to the inspector and you want to make sure that you set
them to static navigation, static, so that we can put the navigation mesh on there and bake it in.
So just click on the bake button in the navigation tab.
The other thing you want to do with those two goals, if we select them in the inspector, is just turn
off their box.
Collider We just want them as a destination location, nothing to collide with and nothing in the nav
mesh.
Okay, so now we're ready with that.
We're going to create an air controller script.
So create yourself a new C-sharp script called I Control.
And we're going to open that up and put some code in for these agents.
Okay.
So in this code, we want the unity engine AI in our libraries up here.
It's going to stay a mono behavior.
These are going to go onto the capsules, all the agents.
So when we've got them, we need to set them up with a goal location to get to.
So we'll go a public game object goal and then we need our NAV agent to get the agent that's on those
objects.
When we add them, then inside of the start will go agent equals this dot get component nav mesh agent.
And then with that agent we'll go agent set destination to the goal dot transform dot position.
So that's pretty much all we need in here for now.
So save that now we'll go back into.
Unity.
And what we'll do with those scripts is to put them on to a blue agent.
So we're going to drag and drop that onto our blue agent.
And we also want to drop it onto the red agent.
Okay, now let's go back to the inspector now for the blue agent and the red agent, which I can just
select and add this to them both is a nav mesh agent component.
Let me scroll this up so that you can see what's going on down here.
Okay.
So the basic settings for our nav mesh agent that we've been using before, the speed, you might want
to speed them up a little bit faster to see the simulation.
So I'm going to put their speed up to ten.
Okay.
Now selecting the blue agent, I will set the goal here to the blue goal and then the red agent will
be set to the red goal like that there.
All right.
So let's now just press play and have a look at what happens when they are asked to go to a new destination.
Okay.
So they're going to go up.
They go through the corridor and over to their goals.
At which point they will stop.
All right.
So while we're here, let's just set that main camera to game object, align with view so that we can
see it when I press play and have to switch back to the scene.
What we're going to do with our agents, so is our blue agent.
And our red agent is just shift, select them and hit control D to make a few duplicates of them like
that.
Now they'll all be in exactly the same space, but once they start moving, they'll all sort of pop
apart because of their colliders.
Let's press play and have a look if we can get those flow lines and that self-organization to begin.
Okay, so here they come and they're going along here.
And you can see that they did.
Okay, so they've stayed together.
Now, we haven't even told them to stay together.
It's just the way that it's actually working this this flow lines.
It's like when people are in a crowd and they're moving, they tend to sort of go with the least resistant
path, even though you haven't really told them to do.
It's almost like water self organizing as well when it's flowing in different directions.
So let's add a few more agents.
We'll just shift, select those and then go control D, control D and press play and we'll have a look
what this does.
Okay.
So they're coming around and there's a little bit of turbulence.
So that's the turbulence coming in because we now have basically over six characters and they go to
each end.
So this is actually a fun simulation to have a bit of a play with and experiment with the sort of functions
that happened within the flow of a crowd.
So feel free to add more capsules of different colours with different goal locations if you want to
have a bit of a play around with it.
You can modify this simulation as much as you like by adding more walls and more agents.
Here you can see I've got four different coloured agents and even when they crowd you can clearly see
the flow lines forming in IE.
When this type of behaviour occurs in a system that's not specifically programmed to do this, it's
called emergent behaviour and it's these clever, lifelike results we are 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.