WEBVTT

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

1
00:16.370 --> 00:22.550
less as individuals and more on mass. Crowd behavior differs from individual behavior in that properties

2
00:22.550 --> 00:29.180
within the crowd (such as emotion, collision, direction and speed) propagate from one person to the next

3
00:29.420 --> 00:35.870
giving the entire group its own identity. To simulate every single individual in such a group and then

4
00:35.870 --> 00:38.260
produce a crowd would be labor intensive

5
00:38.390 --> 00:43.160
and in fact many of the things you might consider important decision-makers for individual movement

6
00:43.250 --> 00:47.410
in a crowd can be boiled down to simple vector mathematics.

7
00:47.450 --> 00:52.610
There are numerous methods researchers have investigated for simulating crowds, including having people

8
00:52.610 --> 00:57.270
move like particles in a fluid to using real world data.

9
00:57.310 --> 01:03.320
A crowd doesn't move as a chaotic set of individuals but rather in a quasi organized fashion.

10
01:03.410 --> 01:09.480
It is said to be a self-organizing system where flow lines appear in the direction of movement.

11
01:09.500 --> 01:14.990
However, it has been found that if a crowd becomes denser than six people per square meter, then pockets

12
01:14.990 --> 01:16.430
of turbulence will occur.

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

14
01:22.280 --> 01:26.790
has been used in motion pictures to simulate animal herds and armies.

15
01:26.840 --> 01:33.140
It's based on 3 simple rules. Turn towards the average heading of the group,  turn towards the average

16
01:33.140 --> 01:36.830
center of the group and avoid those around you.

17
01:36.850 --> 01:42.700
We will be using these rules in combination with navmeshes and recasting to create crowds and flocks

18
01:42.710 --> 01:44.040
in this section.

19
01:44.330 --> 01:49.910
If you'd like to explore some simple crowd dynamics, you can do so using a navmesh.

20
01:50.300 --> 01:53.750
Open up Unity and create a ground plane.

21
01:53.830 --> 01:55.810
Also create 2 walls.

22
01:55.820 --> 02:05.110
So I've just made the ground plane out of a normal 3D plane and I've created two walls out of some cubes

23
02:05.540 --> 02:10.430
and the walls are positioned such that there's a corridor down the middle.

24
02:10.430 --> 02:18.620
The blue capsule that I've added over here is going to have to walk up here, down the corridor and

25
02:18.710 --> 02:20.300
over to this cube.

26
02:20.720 --> 02:28.280
This capsule here is going to have to walk up and around to there. So create 2 capsules so they're just in the

27
02:28.280 --> 02:33.020
Hierarchy. '3D Object > Capsule' and I've colored them differently

28
02:33.020 --> 02:38.170
and then I've named one RedAgent and one BlueAgent.

29
02:38.420 --> 02:47.270
I've also added in some goals. I've added a cube called BlueGoal and that cube is at the opposite

30
02:47.330 --> 02:54.740
end of the path that we're going to create for the BlueAgent...and RedGoal is at the opposite end for

31
02:54.950 --> 02:56.360
the RedAgent.

32
02:56.360 --> 03:01.400
Now RedAgent needs a navmesh added to it.

33
03:01.400 --> 03:03.350
These are just all the default settings.

34
03:03.350 --> 03:08.350
BlueAgent also has a navmesh added to it

35
03:08.510 --> 03:16.130
and then on both of these, I've got a script called 'AIController' and in that script

36
03:16.190 --> 03:17.810
you'll find this.

37
03:17.810 --> 03:23.630
First of all, I've an exposed variable (public variable) called 'goal' and that's what we will give the

38
03:23.630 --> 03:29.480
agents, the 2 goal cubes, so that they know where they're heading towards.

39
03:29.480 --> 03:36.190
After that we're grabbing hold of the NavMeshAgent that's attached to our character.

40
03:36.320 --> 03:43.220
Don't forget to include 'using UnityEngine.AI;' library at the top. In the Start, we

41
03:43.220 --> 03:49.760
then grab the agent attached to the capsule and we could also at this point, set the

42
03:49.760 --> 03:56.500
destination goal which will be 'goal.transform.position' and that's all we need to do.

43
03:56.540 --> 04:06.950
We can even get rid of the Update because we won't need it. Save that and attach that code to

44
04:06.980 --> 04:11.070
RedAgent and BlueAgent.

45
04:11.250 --> 04:20.490
Then you want to select the plane and the two cubes that are the walls and ensure that they are set to

46
04:20.490 --> 04:26.030
Navigation Static and you can do that in the Navigation > Object tab

47
04:26.130 --> 04:32.860
by selecting this checkbox here or you can also set them in the Inspector as we have before.

48
04:32.940 --> 04:38.640
Once that's all set up, you want to go to Bake and then click on the [Bake] button and that will give you

49
04:38.970 --> 04:40.030
a blue area

50
04:40.040 --> 04:41.370
like you're seeing here.

51
04:41.430 --> 04:45.240
So the only parts that aren't walkable are where the walls are.

52
04:45.240 --> 04:49.860
This will force the agents to have to go from one end to the other.

53
04:50.280 --> 05:00.350
Now to make sure that happens, select BlueAgent and then in the Inspector, on the code for the AIController,

54
05:00.720 --> 05:04.650
you want to use the cube that's over here.

55
05:04.770 --> 05:05.150
Sorry,

56
05:05.160 --> 05:10.020
next to RedAgent as BlueAgent's goal.

57
05:10.230 --> 05:17.910
So grab that cube (which I've called BlueGoal) and then you would drag-and-drop that into the

58
05:17.910 --> 05:22.820
goal slot for the blue capsule. For the red capsule,

59
05:22.830 --> 05:24.200
it's the other way around.

60
05:24.240 --> 05:30.410
The goal will be the cube at the other end.

61
05:30.420 --> 05:41.100
Now when you play this, the agents will walk the path and swap over from each other.

62
05:41.110 --> 05:43.860
Now that was sped up by the magic of video editing

63
05:43.870 --> 05:49.750
but if you want your agents to move faster, don't forget that you can select them in the Hierarchy, go over

64
05:49.750 --> 05:53.390
to the Inspector and change their Speed value.

65
05:53.540 --> 05:57.450
Mine are currently set to 10, which is quite fast by itself.

66
05:57.580 --> 06:01.440
When we start adding more agents, you might want it to run a little bit slower.

67
06:01.630 --> 06:10.930
So, first of all, let's have a look at that phenomenon of 'self-organizing by flow', which is where 2 groups

68
06:10.930 --> 06:18.940
coming together, moving in opposite directions, will form lines of flow through the crowd.

69
06:18.940 --> 06:27.460
All we need to do is select both of our agents and then Ctrl-D and create a whole heap of copies.

70
06:27.460 --> 06:36.690
Let's create 20 copies. Now with those in place, let's press Play and we can watch them.

71
06:36.750 --> 06:41.310
They're all in the same spot, so they're going to explode from the same position that they're in. Now

72
06:41.340 --> 06:42.770
as they come together,

73
06:44.290 --> 06:46.450
they will form lines of flow

74
06:46.450 --> 06:52.290
and that's only a small group, so you get 2 clear lines of flow where they overlap.

75
06:52.570 --> 07:00.640
From here, you could select all of those duplicates and duplicate them twice again to get some more

76
07:01.000 --> 07:04.700
and then press Play. Again,

77
07:04.730 --> 07:11.870
they explode out from the single place and now they're going to crowd in this area here and we will

78
07:11.870 --> 07:14.700
find that they will form lines of flow.

79
07:15.550 --> 07:22.180
You get a little bit of turbulence, as you saw there, where it got quite dense and some guys back here actually

80
07:22.180 --> 07:25.670
got stuck in that crowd as well.

81
07:25.720 --> 07:30.710
You can modify the simulation as much as you like, by adding more walls and more agents.

82
07:30.730 --> 07:36.610
Here can see I've got 4 different colored agents and even when they crowd you can clearly see the flow

83
07:36.610 --> 07:38.490
lines forming.

84
07:38.510 --> 07:44.800
In A.I., when this type of behavior occurs in a system that's not specifically programmed to do this, it's called

85
07:44.800 --> 07:48.300
emergent behavior and it's these clever lifelike results

86
07:48.320 --> 07:54.850
we're trying to get in our NPCs with as minimum programming effort as possible. In the next lecture we

87
07:54.850 --> 07:58.740
will work on a crowd simulation with animated human characters.