WEBVTT

00:02.300 --> 00:07.490
In this section, we will begin looking at the simulation of a group of agents in which they are treated

00:07.490 --> 00:10.350
less as individuals and more en masse.

00:10.370 --> 00:15.500
Crowd behavior differs from individual behavior in that properties within the crowd, such as a motion,

00:15.500 --> 00:21.920
collision, direction and speed propagate from one person to the next, giving the entire group its

00:21.920 --> 00:28.640
own identity to simulate every single individual in such a group and then produce a crowd would be labor

00:28.640 --> 00:29.420
intensive.

00:29.420 --> 00:34.340
And in fact, many of the things you might consider important decision makers for individual movement

00:34.340 --> 00:38.210
in a crowd can be boiled down to simple vector mathematics.

00:38.450 --> 00:43.760
There are numerous methods researchers have investigated for simulating crowds, including having people

00:43.760 --> 00:47.780
move like particles in a fluid to using real world data.

00:48.380 --> 00:54.020
A crowd doesn't move as a chaotic set of individuals, but rather in a quasi organized fashion.

00:54.410 --> 01:00.230
It is said to be a self-organizing system where flow lines appear in the direction of movement.

01:00.530 --> 01:06.140
However, it has been found that if a crowd becomes denser than six people per square metre, then pockets

01:06.140 --> 01:07.600
of turbulence will occur.

01:07.610 --> 01:13.430
One of the most popular and simplest ways to simulate a crowd is with Reynolds flocking algorithm that

01:13.430 --> 01:17.570
has been used in motion pictures to simulate animal herds and armies.

01:17.810 --> 01:24.320
It's based on three simple rules turn towards the average heading of the group, turn towards the average

01:24.320 --> 01:27.920
center of the group and avoid those around you.

01:27.950 --> 01:33.350
We will be using these rules in combination with NAV measures and rate casting to create crowds and

01:33.350 --> 01:34.760
flocks in this section.

01:34.760 --> 01:42.140
So to begin our exploration of crowds, we're going to create a really simple crowd here on a nav mesh

01:42.140 --> 01:49.100
so that we can investigate the principles of flow and how there's self-organization within that.

01:49.100 --> 01:53.660
What I'd like you to do is create a new unity project.

01:53.720 --> 01:58.550
And inside of it, we're going to add just a simple plane like this.

01:58.550 --> 02:01.520
Now, I've just put a texture on there so that you can see it better.

02:01.550 --> 02:08.960
I've created two wall objects and these were just cubes that I've stretched out, so I've basically

02:08.960 --> 02:17.690
just selected them and stretched them in different ways so that you get these two walls like that.

02:17.690 --> 02:23.060
Now we also need a goal location for our two agents.

02:23.060 --> 02:25.730
We've got a blue capsule and a red capsule.

02:25.730 --> 02:27.770
These are going to be our nav mesh agents.

02:28.100 --> 02:34.430
I need a cube added here, which will be called red goal, as you can see over in the hierarchy and

02:34.430 --> 02:37.220
this one here, which will be the blue goal.

02:37.220 --> 02:43.250
What we're going to do is get our agents to travel through this course to get to their particular goal.

02:43.580 --> 02:46.100
Next, you can create the agent.

02:46.100 --> 02:48.350
So the agents are just capsules.

02:48.350 --> 02:53.600
And to those capsules I've added a blue material and a red material.

02:53.600 --> 02:57.110
Now there's some other files going on down in my assets.

02:57.110 --> 02:58.370
Don't you worry about those.

02:58.370 --> 03:01.490
We'll get to those in the next lecture.

03:01.490 --> 03:02.420
Just for now.

03:02.420 --> 03:04.460
We aren't needing them.

03:04.850 --> 03:11.510
Okay, so once you've got that particular set up working, we're going to make a mesh.

03:11.510 --> 03:16.820
So we need to select the plane, which is the ground and the walls.

03:17.600 --> 03:23.930
And from that, we're going to then go over to the inspector and you want to make sure that you set

03:23.930 --> 03:31.640
them to static navigation, static, so that we can put the navigation mesh on there and bake it in.

03:32.090 --> 03:36.050
So just click on the bake button in the navigation tab.

03:36.050 --> 03:41.330
The other thing you want to do with those two goals, if we select them in the inspector, is just turn

03:41.330 --> 03:42.110
off their box.

03:42.110 --> 03:49.520
Collider We just want them as a destination location, nothing to collide with and nothing in the nav

03:49.520 --> 03:50.210
mesh.

03:50.390 --> 03:53.240
Okay, so now we're ready with that.

03:53.240 --> 03:57.080
We're going to create an air controller script.

03:57.080 --> 04:02.510
So create yourself a new C-sharp script called I Control.

04:02.510 --> 04:08.090
And we're going to open that up and put some code in for these agents.

04:08.750 --> 04:09.200
Okay.

04:09.200 --> 04:14.180
So in this code, we want the unity engine AI in our libraries up here.

04:14.180 --> 04:15.860
It's going to stay a mono behavior.

04:16.640 --> 04:20.780
These are going to go onto the capsules, all the agents.

04:20.780 --> 04:24.500
So when we've got them, we need to set them up with a goal location to get to.

04:24.500 --> 04:33.350
So we'll go a public game object goal and then we need our NAV agent to get the agent that's on those

04:33.350 --> 04:33.740
objects.

04:33.740 --> 04:42.890
When we add them, then inside of the start will go agent equals this dot get component nav mesh agent.

04:44.390 --> 04:54.590
And then with that agent we'll go agent set destination to the goal dot transform dot position.

04:55.820 --> 04:58.520
So that's pretty much all we need in here for now.

04:58.520 --> 05:01.730
So save that now we'll go back into.

05:01.900 --> 05:02.560
Unity.

05:02.740 --> 05:08.500
And what we'll do with those scripts is to put them on to a blue agent.

05:08.500 --> 05:11.320
So we're going to drag and drop that onto our blue agent.

05:11.860 --> 05:15.640
And we also want to drop it onto the red agent.

05:16.970 --> 05:23.290
Okay, now let's go back to the inspector now for the blue agent and the red agent, which I can just

05:23.290 --> 05:29.350
select and add this to them both is a nav mesh agent component.

05:29.650 --> 05:33.970
Let me scroll this up so that you can see what's going on down here.

05:34.090 --> 05:34.570
Okay.

05:34.570 --> 05:40.780
So the basic settings for our nav mesh agent that we've been using before, the speed, you might want

05:40.780 --> 05:43.540
to speed them up a little bit faster to see the simulation.

05:43.540 --> 05:46.300
So I'm going to put their speed up to ten.

05:46.390 --> 05:46.830
Okay.

05:46.840 --> 05:59.500
Now selecting the blue agent, I will set the goal here to the blue goal and then the red agent will

05:59.500 --> 06:04.690
be set to the red goal like that there.

06:04.990 --> 06:05.410
All right.

06:05.410 --> 06:14.260
So let's now just press play and have a look at what happens when they are asked to go to a new destination.

06:16.280 --> 06:16.550
Okay.

06:16.550 --> 06:17.630
So they're going to go up.

06:17.630 --> 06:23.210
They go through the corridor and over to their goals.

06:24.400 --> 06:26.200
At which point they will stop.

06:26.590 --> 06:26.860
All right.

06:26.860 --> 06:32.260
So while we're here, let's just set that main camera to game object, align with view so that we can

06:32.260 --> 06:35.210
see it when I press play and have to switch back to the scene.

06:35.230 --> 06:38.290
What we're going to do with our agents, so is our blue agent.

06:38.290 --> 06:46.030
And our red agent is just shift, select them and hit control D to make a few duplicates of them like

06:46.030 --> 06:46.480
that.

06:46.480 --> 06:50.560
Now they'll all be in exactly the same space, but once they start moving, they'll all sort of pop

06:50.560 --> 06:52.360
apart because of their colliders.

06:52.480 --> 06:59.140
Let's press play and have a look if we can get those flow lines and that self-organization to begin.

06:59.230 --> 07:02.710
Okay, so here they come and they're going along here.

07:03.220 --> 07:05.350
And you can see that they did.

07:05.380 --> 07:07.390
Okay, so they've stayed together.

07:07.390 --> 07:10.630
Now, we haven't even told them to stay together.

07:10.990 --> 07:14.860
It's just the way that it's actually working this this flow lines.

07:14.870 --> 07:21.520
It's like when people are in a crowd and they're moving, they tend to sort of go with the least resistant

07:21.520 --> 07:24.070
path, even though you haven't really told them to do.

07:24.080 --> 07:28.570
It's almost like water self organizing as well when it's flowing in different directions.

07:28.570 --> 07:30.760
So let's add a few more agents.

07:30.760 --> 07:39.940
We'll just shift, select those and then go control D, control D and press play and we'll have a look

07:39.940 --> 07:41.290
what this does.

07:44.340 --> 07:44.760
Okay.

07:44.760 --> 07:49.110
So they're coming around and there's a little bit of turbulence.

07:49.110 --> 07:55.860
So that's the turbulence coming in because we now have basically over six characters and they go to

07:55.860 --> 07:56.520
each end.

07:56.520 --> 08:03.930
So this is actually a fun simulation to have a bit of a play with and experiment with the sort of functions

08:03.930 --> 08:07.470
that happened within the flow of a crowd.

08:07.470 --> 08:13.020
So feel free to add more capsules of different colours with different goal locations if you want to

08:13.020 --> 08:14.850
have a bit of a play around with it.

08:16.730 --> 08:21.650
You can modify this simulation as much as you like by adding more walls and more agents.

08:21.680 --> 08:27.200
Here you can see I've got four different coloured agents and even when they crowd you can clearly see

08:27.200 --> 08:30.230
the flow lines forming in IE.

08:30.260 --> 08:35.540
When this type of behaviour occurs in a system that's not specifically programmed to do this, it's

08:35.540 --> 08:42.020
called emergent behaviour and it's these clever, lifelike results we are trying to get in our NPCs

08:42.020 --> 08:44.780
with as minimum programming effort as possible.

08:44.810 --> 08:49.730
In the next lecture we will work on a crowd simulation with animated human characters.
