WEBVTT

0
00:10.980 --> 00:16.890
As I mentioned in the previous lecture, developing the A.I. for a crowd of NPCs is far simpler than most

1
00:16.890 --> 00:21.930
people think. This is because from simple rules, complex behavior can develop

2
00:22.140 --> 00:28.170
and although we aren't programming the so-called brains of the NPCs, they still appear to move with some

3
00:28.170 --> 00:30.020
level of intelligence.

4
00:30.150 --> 00:37.530
In this lecture, we will explore crowds further with a group of animated characters. To follow along, download

5
00:37.530 --> 00:44.070
the 'StreetSceneStarter' that comes with this lecture. As you'll see when you open up the scene (which

6
00:44.070 --> 00:45.320
is called Crowd)

7
00:45.390 --> 00:49.580
there's a group of people in there and they're standing on a plane.

8
00:50.040 --> 00:56.180
If you go to your Navigation window, you'll find that there's a navmesh that's already being baked in

9
00:56.180 --> 00:56.540
there.

10
00:56.730 --> 00:59.880
So we're not after a complex environment at this point,

11
00:59.880 --> 01:03.310
we're just after something where people can walk around.

12
01:03.340 --> 01:11.460
Now each character has been downloaded from Mixamo and they've got an animation on them already.

13
01:11.460 --> 01:19.890
So if you select, for example, this girl here (Girl_3) you'll find that they have an Animator Controller on

14
01:19.890 --> 01:20.440
them

15
01:20.520 --> 01:23.140
and I set that controller up for you.

16
01:23.310 --> 01:27.190
So if you just double-click to open it up and have a look what is in there, you'll find

17
01:27.210 --> 01:35.040
they have an idle animation, a walking animation and a running animation and these animations are set

18
01:35.040 --> 01:37.170
up to trigger between each other.

19
01:37.170 --> 01:41.190
So I've created transitions from Idle to Walking and then back again

20
01:41.430 --> 01:43.390
and the same for all the other states.

21
01:43.650 --> 01:48.850
And then for each one, for the transitions, we've got triggers.

22
01:48.870 --> 01:53.710
So we've got an 'isRunning' trigger, an 'isWalking' trigger and an 'isIdle' trigger

23
01:53.760 --> 02:02.320
and we'll be controlling these and when they trigger from some code. The characters themselves also have

24
02:02.350 --> 02:10.020
a navmesh agent attached that you'll find in the Inspector and their speed is set to 2.

25
02:10.240 --> 02:17.580
So, in this case, they've only got animations that I'm having them walk and to stand idle. There's also a

26
02:17.590 --> 02:20.350
run as well, so they're forward moving animations.

27
02:20.440 --> 02:26.620
There's no 'strafe set' set up because this movement isn't particularly complex as far as it's quite

28
02:26.620 --> 02:27.380
simple

29
02:27.460 --> 02:30.950
and they're moving in a straight line when they do animate forward

30
02:31.030 --> 02:37.480
and if you can coordinate the speed of your Nav Mesh Agent with the speed that's in the animation

31
02:37.480 --> 02:43.240
itself, then it's fairly believable and it's a lot simpler set up for us.

32
02:43.270 --> 02:48.430
I guess I wanted to show you a different way of doing this as well if you don't want to go to the

33
02:48.430 --> 02:54.640
effort of the complexity of setting up a 'strafe set', or you don't have the skills to create all of those

34
02:54.640 --> 02:56.090
different animations.

35
02:56.290 --> 02:59.020
So the speed is set to 2 for all of them.

36
02:59.020 --> 03:06.100
Now if we press Play they'll go straight into their Idle animation and they're all synchronized, so

37
03:06.100 --> 03:08.840
they look like they're a flash mob at the moment!

38
03:08.980 --> 03:14.100
To begin with, we're going to get them walking towards a goal.

39
03:15.180 --> 03:19.870
We will determine the goal by placing a cube into the scene.

40
03:20.040 --> 03:28.140
So go to the Hierarchy and right-click and create a new cube and then we'll just move that somewhere

41
03:28.170 --> 03:28.920
on the navmesh.

42
03:28.940 --> 03:31.620
So let's just put it over here.

43
03:31.620 --> 03:38.730
While we're at it, we'll create another one as well so they can move between locations, so Ctrl-D and

44
03:38.730 --> 03:42.070
then move another cube back to there.

45
03:42.090 --> 03:50.910
They'll kind of move between these two goals. I might rename these 'Goal' (so that we know what they

46
03:50.910 --> 03:51.520
are)

47
03:51.930 --> 03:56.040
and you can delete the renderers on those as well

48
03:56.100 --> 04:00.960
if you don't want to see them. I'll also get rid of their collision because we don't want that interfering

49
04:01.020 --> 04:02.210
with anything.

50
04:02.250 --> 04:06.290
Now in order to find the goals, we will tag them.

51
04:06.420 --> 04:14.160
So go over to the Inspector and with the goals selected, in the Tag dropdown, tag

52
04:14.160 --> 04:15.770
them as goal.

53
04:15.810 --> 04:20.610
If you don't have 'goal' as a tag, you'll have to add that and then set it to 'goal'.

54
04:20.610 --> 04:27.610
Now make sure you note the spelling...this is lower-case 'goal', which is what we will reference in our code.

55
04:27.630 --> 04:33.870
We're now going to add some code to control the movement of the characters. Go to the Assets folder and

56
04:33.890 --> 04:40.770
in there you'll find an 'AIControl' script that I've included with this starter file. Just open that up and

57
04:40.770 --> 04:41.530
have a look.

58
04:41.580 --> 04:45.460
Now it's should be pretty familiar because we've done all these things before.

59
04:45.660 --> 04:53.190
We're first of all setting up an array of goal locations (which will be those cubes) and then we're also

60
04:53.190 --> 04:57.690
grabbing hold of the NavMeshAgent that is on our characters.

61
04:57.690 --> 05:05.730
At that point, in the Start function, we're going to get all of the locations that are in the scene that

62
05:05.730 --> 05:08.950
have the tag 'goal' and that's why it's important

63
05:08.940 --> 05:14.670
you've got 'goal' tagged with exactly the same spelling/case as this on those cubes and you can add as many as you

64
05:14.670 --> 05:16.250
like into the scene.

65
05:16.380 --> 05:22.660
Then the agent is grabbing hold of its navmesh

66
05:23.400 --> 05:35.790
and then the agent is having its destination set to a random location in that array.

67
05:35.800 --> 05:44.080
So we're using a Random.Range on the length of the goalLocations array just to pluck one out and then getting

68
05:44.080 --> 05:49.620
hold of its position, which will then be the agent's destination.

69
05:49.690 --> 05:57.460
So save that and we're going to attach that to each of our characters. So select them all and

70
05:57.460 --> 06:03.580
then drag-and-drop the 'AIControl' script onto them in the Inspector.

71
06:03.700 --> 06:09.940
Now press Play and you'll be able to see the characters picking a location to go to.

72
06:09.940 --> 06:12.870
So they're randomly picking their location and moving to it.

73
06:12.910 --> 06:19.400
What you'll also notice at this point is that they are staying in the idle animation.

74
06:19.570 --> 06:24.820
So if you want to synchronize your animation with your character's movement or whatever they're doing,

75
06:24.850 --> 06:30.550
you need to trigger one of those transitions that I showed you before. To do that,

76
06:30.550 --> 06:36.590
let's go back into the code and we'll need to grab the animator component that's on our character,

77
06:36.730 --> 06:46.480
just like we had to grab the agent for the navmesh. So...'Animator anim;' then in the Start, you'll

78
06:46.480 --> 06:54.420
also have to set it up so 'anim = this.GetComponent' with Animator

79
06:54.620 --> 07:00.660
and then at the start because they're  walking straight away, we can trigger. So to do that we go

80
07:00.760 --> 07:12.360
'anim.SetTrigger("isWalking");' and save that. This here has to be exactly

81
07:12.360 --> 07:17.200
the same spelling and capitalization as the animator.

82
07:17.340 --> 07:21.830
...these values here.

83
07:21.860 --> 07:30.090
So 'is' with a little 'i' and 'W' with a capital. Now if you press Play, you'll find that they go straight

84
07:30.090 --> 07:36.840
into the walking animation and they'll choose a goal at random to go to and walk over to it and then walk

85
07:36.840 --> 07:40.360
around it because they're not told to stop walking once they get there.

86
07:41.310 --> 07:47.010
So, stop playing that. Now if you want another goal, you can just select your goal from the Hierarchy,

87
07:47.010 --> 07:51.820
Ctrl-D to duplicate it and then you can add another one...

88
07:52.440 --> 07:55.000
and they're already coded to pick them at random.

89
07:55.000 --> 08:01.370
So you'll find that they've randomly not picked the new one.

90
08:01.460 --> 08:03.240
Let's just press Play again.

91
08:04.170 --> 08:09.390
There we go, some people have now.

92
08:09.570 --> 08:15.210
What we're going to do is when they get to their random location, we want them to pick another random

93
08:15.210 --> 08:22.020
location and go to that. Figuring out when you're at a location and then picking another location to go

94
08:22.020 --> 08:28.620
to is very much the same steps that we took when we were moving the tank around and patrolling between

95
08:28.620 --> 08:32.300
different waypoints, so it's the same kind of logic.

96
08:32.300 --> 08:34.390
Open up your code. In the Update,

97
08:34.560 --> 08:40.080
what we'll do is test whether we've reached the destination (or if we're in a certain distance

98
08:40.440 --> 08:44.050
of the destination, our goal) and then we'll pick another one.

99
08:44.070 --> 08:48.680
So, the code, I'll just paste in here and then go through it.

100
08:49.020 --> 08:55.980
It says 'if the agent remaining distance', so the Nav Mesh Agent has this value called 'Remaining Distance'

101
08:55.980 --> 09:03.270
which is keeping track of how close the agent is to the destination goal.

102
09:03.270 --> 09:11.760
Now if the agent becomes < 1 distance away from the goal cube, then it's going to set another

103
09:11.760 --> 09:13.150
random location

104
09:13.440 --> 09:21.420
and the setting of the random location (this code here) is exactly the same code that we picked the first

105
09:21.420 --> 09:24.710
random location with, up here.

106
09:24.900 --> 09:27.060
So that's all the lines that you need

107
09:27.150 --> 09:33.780
and if you save that and switch back to Unity and press Play, you'll now find that the agents will get

108
09:33.840 --> 09:38.850
to a location and then they'll turn around and move somewhere else.

109
09:38.850 --> 09:46.900
The turning of the actual agent...if we select one of them that's moving, you'll be able to see if we go

110
09:46.900 --> 09:53.740
to the Scene view that the Nav Mesh Agent code itself does all of the turning for you.

111
09:54.060 --> 10:00.580
Previously, when we were working with the tank and turning it we would have to perform a slerp and then

112
10:00.580 --> 10:02.040
push the tank forward.

113
10:02.040 --> 10:08.860
So this is the navmesh doing all that for us. We just keep the animation in forward walking and it's

114
10:08.860 --> 10:14.610
going to react really nicely for us, without us doing any extra effort.

115
10:19.600 --> 10:27.520
Now if you want a larger crowd, then you just have to shift-select all of the characters and just

116
10:27.520 --> 10:32.920
Ctrl-D to duplicate them and then you might just want to move them away from each other a little

117
10:32.920 --> 10:33.190
bit,

118
10:33.190 --> 10:39.220
just like that and then if you press Play, they're still going to behave

119
10:39.240 --> 10:44.280
in the same way, because they've got the same code...they're on the same navmesh and they have the same

120
10:44.280 --> 10:48.590
set of goals to select from. With what we've got

121
10:48.610 --> 10:55.360
(and before we move on to the next lecture) what I want is to set this street scene up so that the people are

122
10:55.450 --> 10:59.830
moving up and down the sidewalk, like you would find in a city. First of all,

123
10:59.900 --> 11:05.710
I'm just going to remove this goal (because we don't need that) and I'll grab this goal and put it up one

124
11:05.710 --> 11:07.040
end of the street

125
11:08.060 --> 11:16.940
and this goal and put it at the other end. So they're just going to walk up and down this part of the

126
11:16.940 --> 11:22.790
sidewalk. To prevent them going in here (which is where you might have buildings or something) we can

127
11:22.790 --> 11:25.120
just simulate those buildings with a cube.

128
11:25.130 --> 11:30.820
So let's create a cube and I'll bring that over here

129
11:32.110 --> 11:33.540
and resize it...

130
11:37.430 --> 11:43.400
and when you've got something that you're happy with, you can select it, go over to the Navigation window

131
11:43.610 --> 11:46.540
and we want to make this a 'not walkable' area.

132
11:46.700 --> 11:53.480
So under the Object tab, you can make it a Navigation Static, but in the Navigation Area, you can drop

133
11:53.480 --> 11:56.860
that down and say that it's 'Not Walkable'.

134
11:56.990 --> 12:05.990
Then when you go back to Bake, it's going to exclude this, so that when the people start to walk (if we

135
12:05.990 --> 12:10.250
press Play) then they will be going up and down the sidewalk...

136
12:13.300 --> 12:15.370
and avoiding this area here.

137
12:15.380 --> 12:22.600
Some of them actually escaped, we might say, around the edge so you could just grab hold of this cube

138
12:22.630 --> 12:24.070
and make it a lot wider

139
12:24.100 --> 12:30.640
so it fills up that entire block and that will dissuade them from taking the shortcut and getting them

140
12:30.640 --> 12:31.060
all over

141
12:31.060 --> 12:36.190
here. Of course, if you started all your characters over here then they're not going to go all the way around

142
12:36.250 --> 12:36.910
there.

143
12:37.180 --> 12:45.440
So with that resized, if I just go [Bake] again and press Play. There they are, coming back over to that side

144
12:45.540 --> 12:53.430
and we want them walking up and down here for the next lecture, in which we will add some obstacle avoidance.