WEBVTT

0
00:11.160 --> 00:14.540
In this lecture, we will continue working on our crowd simulation.

1
00:14.710 --> 00:19.960
Since the last lecture, I've added some extra scenery into the scene that you'll find in the Street folder

2
00:20.020 --> 00:22.170
in the project under Prefabs.

3
00:22.270 --> 00:28.030
I've also added another goal location so the crowd walks along two sides of the buildings.

4
00:28.090 --> 00:33.520
Now we make some modifications to their animation controls to stop them walking like a regimented Army

5
00:33.840 --> 00:36.670
before we add a dynamic obstacle.

6
00:36.670 --> 00:42.000
First thing that we need to do is to stop them walking in step with each other.

7
00:42.010 --> 00:47.180
Remember, they are playing exactly the same animation at exactly the same speed.

8
00:47.200 --> 00:53.920
What we can do to make that look a little different is to, first of all, make it so that they're not in step

9
00:53.920 --> 00:59.620
with each other, so that they are at different phases through that walking loop and the other thing we

10
00:59.620 --> 01:04.260
can do is to change how fast or how slow they're walking.

11
01:04.510 --> 01:09.480
So the first thing we want to do is find our animation.

12
01:09.580 --> 01:14.080
Let's click on Assets > People and then there's the 'Crowd'.

13
01:14.080 --> 01:17.620
Double-click on that and this is the Animation Controller

14
01:17.730 --> 01:19.810
with our 'Walking' in it.

15
01:19.840 --> 01:26.410
If you select the Walking animation and look in the Inspector, you'll see that there is a

16
01:26.410 --> 01:34.300
Speed (that we'll come back to in a moment) and a 'Cycle Offset'. This Cycle Offset is a value between 0 and 1

17
01:34.300 --> 01:40.140
which specifies where in the cycle the animation is playing.

18
01:40.150 --> 01:46.520
Now if you set that to random values for all the characters, they will become out of step with each other.

19
01:46.680 --> 01:52.590
To do that easily, we need a parameter that we can grab from our code.

20
01:52.600 --> 01:58.770
So, first of all, we go over to the Parameters section and then click on '+'.

21
01:58.780 --> 02:06.250
We're going to add a Float and we will call that float wOffset and that's because it's a walking

22
02:06.250 --> 02:07.090
offset.

23
02:07.570 --> 02:13.510
Then for our walking animation, select that and go over to the Inspector.

24
02:13.660 --> 02:16.370
You want to tick the Parameter box

25
02:16.660 --> 02:17.310
that's right

26
02:17.320 --> 02:23.610
next to Cycle Offset and it will automatically fill in wOffset as the parameter.

27
02:23.620 --> 02:31.360
Now whatever value we give wOffset in our code will come through here and set the offset for the walking.

28
02:32.170 --> 02:41.730
To set that offset, go back to your AIControl code. In the Start, just after we've set up the animator,

29
02:41.950 --> 02:48.910
before we start the walking animation, we will set the offset value for it, so that it's going to be a

30
02:48.910 --> 02:53.770
random value between 0 and 1, going into this wOffset.

31
02:53.980 --> 02:55.390
It's a SetFloat

32
02:55.390 --> 02:58.550
in this case, because it's a float value.

33
02:58.750 --> 03:03.800
So save that, switch back to Unity and press Play

34
03:04.000 --> 03:10.570
and we should now see that our characters won't be walking in-sync with each other, which makes it look

35
03:11.050 --> 03:14.320
a little bit more naturalistic.

36
03:14.320 --> 03:21.550
Now we can also do the same thing with the speed. If we go back to our Walking in the animator, you'll

37
03:21.550 --> 03:24.940
find in the Inspector that there is the Speed value

38
03:24.940 --> 03:25.740
we looked at before.

39
03:25.750 --> 03:32.780
It also can be changed with a parameter and it has a multiplier underneath, that you can see here.

40
03:32.980 --> 03:42.310
Let's create that first of all. So we go over and create a Float and let's call this 'speedMult'.

41
03:42.390 --> 03:45.410
Initially it will be set to 1,

42
03:45.480 --> 03:51.150
otherwise you won't be going anywhere because if you multiply by 0 then it will be going at a speed

43
03:51.150 --> 03:51.930
of 0

44
03:52.260 --> 04:00.750
and then select your Walking animation, click the Parameter box next to it and then change its multiplier

45
04:00.760 --> 04:03.590
to speedMult.

46
04:03.610 --> 04:08.910
Now we can modify speedMult also in our script.

47
04:08.920 --> 04:17.350
So if we open up our script again (the AIControl) and we can do that up in the Start, so we can give them a random

48
04:17.740 --> 04:19.400
walking speed.

49
04:19.420 --> 04:23.730
So let's go

50
04:26.810 --> 04:27.980
anim.SetFloat("speedMult"

51
04:31.150 --> 04:44.480
and then Random.Range, let's say somewhere between 0.5 and 2. Let's put an 'f' on there for our

52
04:44.480 --> 04:45.640
float

53
04:46.020 --> 04:55.930
and save that. At this point, you're going to get only the speed being multiplied in the animation.

54
04:56.180 --> 04:58.540
So it will either slow down or speed up

55
04:58.550 --> 05:08.230
but the agent's speed (the Nav Mesh Agent) isn't being changed and therefore you want to make sure that

56
05:08.230 --> 05:13.940
if you change the animation speed, that you also change the speed at which the Nav Mesh Agent is moving as well.

57
05:13.990 --> 05:21.490
First, we need to save what your multiplier is for your speed, so let's put

58
05:21.490 --> 05:27.520
'float sm =' and we'll take this out of here.

59
05:27.880 --> 05:29.120
Copy that.

60
05:30.130 --> 05:31.470
Paste it up there

61
05:33.100 --> 05:41.350
and then put this as our 'sm' and then we'll also use the 'sm' on our agent speed, so

62
05:42.250 --> 05:49.140
'agent.speed *= sm;' like that.

63
05:49.170 --> 05:56.110
Now we have our speed multiplier applied to our animation AND the agent movement.

64
05:56.160 --> 06:04.560
OK so save that and we'll go back into Unity and we'll play it and we should get different speeds

65
06:04.740 --> 06:06.050
as you can see there

66
06:06.850 --> 06:09.970
and the animations are different as well.

67
06:10.120 --> 06:16.060
So multiplying it by 2 as the maximum, they do walk quite fast but you can

68
06:16.060 --> 06:18.580
see that the animation is keeping up with them.

69
06:18.850 --> 06:23.530
If you want to slow those down because it doesn't look particularly naturalistic, you could have

70
06:23.530 --> 06:33.700
people going along at a leisurely 0.1f pace and then maybe people at say 1.5f.

71
06:33.770 --> 06:35.850
It's up to you

72
06:35.990 --> 06:37.100
what you have in there

73
06:37.340 --> 06:43.850
and the effect that you're actually after, but at least everyone now isn't walking in step with each other

74
06:43.940 --> 06:48.780
and some people are taking a casual stroll and other people are in a hurry.

75
06:48.830 --> 06:56.570
Now we're going to add a dynamic obstacle onto the NavMesh for our agents to move around and that obstacle

76
06:56.780 --> 07:00.870
can be anything, but we're going to make it a first-person controller.

77
07:01.040 --> 07:06.680
So go into your Assets and import the Character package,

78
07:06.920 --> 07:13.400
then once you've done that you'll get a FirstPersonCharacter package down in 'Standard Assets >  Characters'

79
07:13.400 --> 07:15.390
of your Assets folder.

80
07:15.620 --> 07:23.890
Go into Prefabs and grab hold of the FPSController and drag-and-drop that into the Scene.

81
07:24.110 --> 07:32.380
If it's below the surface, just lift it up a little bit and I'm just going to move it back because I'm

82
07:32.380 --> 07:38.080
going to want to walk it into these people going up this way, so we can see it. Now,

83
07:38.110 --> 07:44.950
it has a camera on it which we will use in a moment but for now, we'll use the current camera that's pointing

84
07:44.950 --> 07:52.210
at the scene because I want to walk this around and for you to see that these agents are going to completely

85
07:52.210 --> 07:53.560
ignore it at first.

86
07:53.560 --> 08:04.040
So with that there, press Play and then you can use the W, A, S & D keys to move your character around and

87
08:04.140 --> 08:11.440
you'll notice that you can go straight through people and they don't even care or know that you're

88
08:11.520 --> 08:12.350
there.

89
08:14.180 --> 08:19.340
So if you were playing this in first person mode, the same thing would happen...people would just walk straight

90
08:19.400 --> 08:21.590
up and then straight through you.

91
08:21.780 --> 08:30.490
Ok, so just press Escape to get your mouse control back and stop playing. To make this first person controller

92
08:30.530 --> 08:32.560
into an obstacle on the NavMesh,

93
08:32.580 --> 08:37.950
we add a component to it called a 'Nav Mesh Obstacle'.

94
08:38.090 --> 08:45.410
So select the first person controller in the scene and go over to the Inspector, add component and we're

95
08:45.410 --> 08:48.890
looking for a Nav Mesh Obstacle.

96
08:48.890 --> 08:49.980
Click on that.

97
08:50.010 --> 08:57.470
Now you don't have to make this object static, like we have with all the other geometry because it's

98
08:57.470 --> 09:04.050
not static. It's going to be moving around and the agents are going to react to it. Now

99
09:04.160 --> 09:09.710
within the Nav Mesh Obstacle, you can also set whether it's going to be a Capsule or a Box.

100
09:09.730 --> 09:13.030
Our character is a capsule, so we'll put a capsule on there.

101
09:13.040 --> 09:18.950
At this point you'll see that there's 2 capsules sitting over there. One is the physics collider and

102
09:18.950 --> 09:24.680
one is your NavMesh collider and you can adjust them to be exactly the same. I'll leave them like they are

103
09:24.680 --> 09:26.640
because they're pretty similar.

104
09:26.900 --> 09:37.560
So now when we press Play and we take our character for a walk, we'll find that people avoid it and so

105
09:37.570 --> 09:46.250
they're going around and if we bump into people, we will also affect where they are.

106
09:46.360 --> 09:52.880
So to get a view of what this looks like, hit Escape and stop playing. Let's go

107
09:52.880 --> 09:59.470
down, find our camera that we've got from before and you can delete it or just turn it off.

108
09:59.470 --> 10:08.030
Now when we press play, it'll default over to the first person camera and we can now walk through people

109
10:09.430 --> 10:19.950
and they will avoid us. So you can see this character going around

110
10:20.200 --> 10:23.650
and we also affect those around us as well.

111
10:25.850 --> 10:27.810
You can push them out of the way

112
10:32.160 --> 10:39.240
and as I said before, you can add this Nav Mesh Obstacle to any game object in your scene to make it

113
10:39.240 --> 10:46.140
a dynamic obstacle on the NavMesh, so you could also have a car that you're driving along and have IT

114
10:46.230 --> 10:53.730
as a Nav Mesh Obstacle, so that any agents that are stuck on there will have to navigate around that

115
10:53.730 --> 10:55.230
moving object.

116
10:55.230 --> 11:00.900
Of course, as you can imagine, the more of these obstacles that you add that are dynamic, the more the

117
11:00.900 --> 11:05.380
NavMesh code has to work to keep recalculating the paths.

118
11:05.400 --> 11:11.640
So when you can have something that's static and doesn't move, you want to make it static and think about

119
11:11.640 --> 11:17.690
what's going to move and make it a non-static and add the Nav Mesh Obstacle to it.