WEBVTT

0
00:11.030 --> 00:17.050
In this lecture, we're going to take a closer look at navmesh agents. To begin with, download the

1
00:17.050 --> 00:21.100
NavMeshStationStarter project that comes with this lecture.

2
00:21.250 --> 00:24.940
When you put it up, you'll find an extensive map inside there.

3
00:24.940 --> 00:29.270
This is the map that came with the Angry Bots demo,

4
00:29.500 --> 00:32.070
many versions of Unity ago.

5
00:32.350 --> 00:38.260
I've stripped out most of the code and you'll find just the geometry in there. We'll just use this

6
00:38.320 --> 00:42.340
as a 'play center' for our agents.

7
00:42.370 --> 00:44.740
Now I'm going to zoom in on this bit here and

8
00:44.740 --> 00:49.230
I've also moved my camera so that it's looking down on there when I press Play.

9
00:49.240 --> 00:52.540
So go ahead and select this area here.

10
00:52.690 --> 00:57.140
I've also created 2 materials that you won't have in your stuff. I've created

11
00:57.140 --> 01:03.850
a blue material and a red material. This is just so that we can color the agents as we create them

12
01:03.850 --> 01:07.420
to be able to tell the difference between them.

13
01:07.420 --> 01:14.170
Next we'll set up the navmesh, so make sure you've got your navigation window somewhere in your project. To get that, it's

14
01:14.470 --> 01:21.460
'Window > Navigation'. The next step is to select the geometry in your map that you want to become

15
01:21.460 --> 01:23.260
part of the navmesh.

16
01:23.260 --> 01:31.870
So go around and find the individual pieces of this model (so the individual polygons) and shift-select

17
01:31.870 --> 01:39.220
them until you have most of the area where you want the agents to be able to move.

18
01:39.220 --> 01:45.370
So we want to be able to go down the corridor, so make sure that you add that in and maybe these platforms.

19
01:45.940 --> 01:49.870
If you want them to be able to jump up on things later on, you can add those.

20
01:50.080 --> 01:55.100
If you don't select them all in one go, then you can come back and get them later.

21
01:55.360 --> 02:01.570
Each of the meshes that you have selected needs to be set up to be a Static Navigation.

22
02:01.600 --> 02:07.010
So select the Inspector, still with everything selected in the scene

23
02:07.210 --> 02:14.500
and we want to go up to Static and drop that down and make sure that you've got Navigation

24
02:14.500 --> 02:15.250
Static clicked,

25
02:15.250 --> 02:21.440
just so the navmesh can pick it up. Once you've set those

26
02:21.460 --> 02:22.590
(let me just set it again

27
02:22.590 --> 02:27.990
because I might have turned it off) go back into the Navigation tab

28
02:28.300 --> 02:37.710
and under Bake, click on [Bake]. Now the bits that are part of the navmesh are going to turn blue. At this

29
02:37.710 --> 02:38.180
point,

30
02:38.190 --> 02:44.610
if you've missed anything (so say you want this bit over here to be included) you can come back

31
02:44.640 --> 02:53.430
and select it by itself and go to the Inspector and again, 'Static > Navigation Static', come back

32
02:53.430 --> 02:59.690
to the navigation and under Bake, press [Bake] and that will add it.

33
02:59.940 --> 03:04.780
So you can go through and do one bit at a time and keep adding it.

34
03:04.890 --> 03:11.220
Now if you see something like I've got going on down here, where it's not blue and it's not blue there

35
03:11.280 --> 03:12.030
either,

36
03:12.270 --> 03:15.380
it's just an overlapped rendering problem.

37
03:15.390 --> 03:19.200
So try not to worry about that too much.

38
03:19.200 --> 03:25.620
The issue will be if your agents do not walk over that area, then it hasn't been picked up

39
03:25.620 --> 03:32.100
for the navmesh and just come back, select it again and just make sure that you've got it set to

40
03:32.190 --> 03:37.060
Navigation Static and then 'do a bake'.

41
03:37.080 --> 03:39.410
Now it's time to add in an agent.

42
03:39.420 --> 03:42.070
So let's just zoom out a little bit from here.

43
03:42.270 --> 03:51.820
I'm going to go into the Hierarchy and we'll add in a new '3D Object > Capsule' and this capsule

44
03:51.820 --> 03:59.380
is going to be our A.I. character and let's make that red so we can see

45
03:59.380 --> 04:01.630
it, just drag that over there.

46
04:01.660 --> 04:07.750
Now we're going to need to send this character some commands and we'll do that by tagging it, so that

47
04:07.750 --> 04:09.860
we can find it. In the Inspector,

48
04:09.880 --> 04:16.740
you want to create a new tag called 'ai' (which I've already created) so create that and then set your capsule

49
04:16.740 --> 04:23.510
to be that. At that point, you can then change the name of this. Let's call it a 'RedAgent'.

50
04:25.600 --> 04:30.070
We'll make it into a prefab a little bit later and then you can make multiple copies of them.

51
04:30.070 --> 04:34.170
The important thing at this stage is that it's tagged as 'ai'.

52
04:34.310 --> 04:43.840
I will now create a script called AIControl, so down in the project, create a C# script, call it AIControl

53
04:44.830 --> 04:51.210
and you want to attach that to the RedAgent.

54
04:51.220 --> 04:59.120
Now open it up and inside we want to grab hold of the navmesh agent that is attached to our character.

55
04:59.280 --> 05:09.780
First, we add the library to access that, so 'Using UnityEngine.AI;' then we make a variable

56
05:09.780 --> 05:21.540
to store the link to the agent in. So 'public NavMeshAgent agent;' and then in the start we set it up

57
05:21.540 --> 05:24.760
to be the component that's on it.

58
05:24.780 --> 05:29.360
So 'this.GetComponent' with NavMeshAgent, like that.

59
05:29.400 --> 05:35.970
Now I want to program the agent so that it moves to a quick location on the screen and I could do it

60
05:35.970 --> 05:39.240
down inside of the Update for this particular agent,

61
05:39.330 --> 05:44.910
but if I'm going to have multiple copies of this agent around the place, I don't want them all running

62
05:44.910 --> 05:50.290
their own Update with 'if' statements checking for mouse clicks and that type of thing.

63
05:50.430 --> 05:54.040
So rather than do that, I will create an Agent Manager.

64
05:54.150 --> 05:59.910
Let's go ahead and just remove this Update because I don't want an empty Update processing nothing because

65
05:59.910 --> 06:05.820
it will cause significant lag if you do have a lot of agents trying to run an empty Update

66
06:05.820 --> 06:11.310
and therefore we only need 1 update that will send the message through to all of the agents.

67
06:11.310 --> 06:19.020
So I'm just going to save that, go back over here into the editor and in the Hierarchy, we'll right-click,

68
06:19.020 --> 06:24.790
Create Empty and give it the name of AgentManager

69
06:25.040 --> 06:33.650
and then what I'll also do is create a script called AgentManager. That script gets attached to our

70
06:33.740 --> 06:42.750
AgentManager game object (funnily enough) and then inside of the script, we're going to add this.

71
06:42.800 --> 06:50.000
So this script has an array of agents at the top which gets populated when we start playing.

72
06:50.000 --> 06:56.390
So 'agents' will be filled up with all of the game objects in the scene that have the tag 'ai'. That's why

73
06:56.390 --> 07:03.050
'ai' is so important that you've tagged your agents with it then the clicking on the environment to

74
07:03.050 --> 07:06.880
get the agent to move there is going to happen in the Update.

75
07:06.920 --> 07:13.690
So no matter how many agents you've got tagged as 'ai' in your scene, this message will be sent to them.

76
07:13.700 --> 07:19.640
So if a mouse button is clicked (in this case, the left mouse button) then we'll

77
07:19.670 --> 07:21.300
create a RaycastHit

78
07:21.300 --> 07:30.890
We'll use a physics raycast from the mouse input position via a ScreenPointToRay so it shoots

79
07:30.890 --> 07:36.920
a straight line out from the position of the mouse on the screen into the scene and finds out where it

80
07:36.920 --> 07:40.790
hits, within a distance of 100.

81
07:40.790 --> 07:46.040
If you find you're not hitting anything and you're clicking all over the place, increase

82
07:46.040 --> 07:54.310
this number here, so that you're producing a longer ray. Then inside of the 'if' statement, we've got a foreach

83
07:54.530 --> 07:59.830
and that's just going to loop around all of the agents in the array

84
08:00.020 --> 08:06.620
and for each one of them, it'll grab hold of their AIControl script and get their agent that we just set

85
08:06.620 --> 08:07.930
up for them.

86
08:08.060 --> 08:15.290
Like in the last lecture, we used the SetDestination and that will pass through the position of where

87
08:15.290 --> 08:22.600
the mouse click was. So it's a Vector3 value that's going to go in here, to the navmesh agent

88
08:22.880 --> 08:27.380
and because the navmesh agent's already set up to move around automatically,

89
08:27.380 --> 08:30.170
it will start moving towards that hit point.

90
08:30.170 --> 08:31.980
So let's save that

91
08:32.090 --> 08:37.190
and back in the editor, let's grab hold of our RedAgent which has the script on it, but it doesn't yet

92
08:37.310 --> 08:40.780
have a navmesh agent, so just [Add Component] > Nav Mesh Agent.

93
08:40.800 --> 08:46.630
Don't need to do anything else with that, we'll just leave the default values there.

94
08:46.670 --> 08:56.700
Right, so press Play and we will be able to click somewhere on the area that we defined for our navmesh

95
08:57.920 --> 09:02.120
and you'll see that the agent will now move.

96
09:02.320 --> 09:07.780
Now if you wanted to move a little bit faster, we can select the agent and come down into the Inspector

97
09:07.780 --> 09:10.060
and have a look for the Speed.

98
09:10.060 --> 09:13.600
So it's currently set to 3.5 as default.

99
09:13.600 --> 09:20.960
We can set that to 10, if you want it to go quite fast and press Play.

100
09:20.970 --> 09:28.180
Actually, having said that, I don't think 10 is as fast as you might think. I can also click on here

101
09:28.190 --> 09:33.530
because that's my navmesh and the only way to get into this area is if you go through this little tunnel.

102
09:36.570 --> 09:42.820
Now you saw there that he got a bit stuck, or I should say overshot, where it was going to come down in

103
09:42.820 --> 09:43.590
this path.

104
09:43.600 --> 09:49.120
That's because of the speed and the acceleration and that type of thing as well as the braking.

105
09:49.120 --> 09:51.740
Let me just stop this for a second.

106
09:51.760 --> 09:58.080
What I want to show you is if you go to the Navigation window and select your agent and then press Play

107
09:58.870 --> 10:02.930
you'll be able to see the navmesh and how it works.

108
10:03.130 --> 10:06.990
When you click on the surface, you'll see the path that gets calculated. So let's say you want to

109
10:07.000 --> 10:08.070
go over here.

110
10:08.230 --> 10:15.790
You can see all the polygons that are chosen as part of the path. The agent itself also ends

111
10:15.790 --> 10:21.640
up with some vectors sticking out of it, showing you the direction that it's traveling in and how it's

112
10:21.640 --> 10:24.400
turning, where it's going to be facing towards

113
10:24.400 --> 10:31.210
and those sorts of things as well. Where the agent can move throughout the navmesh is all dependent

114
10:31.450 --> 10:40.040
on the 'Baked Agent Size'. Over in the Navigation window, under Bake, there is a Baked Agent Size

115
10:40.160 --> 10:47.650
that you would have seen before and changing the values on this determines how the navmesh size is generated.

116
10:47.650 --> 10:56.650
So for example, the Agent Radius, if we were to increase that to say that our agent is quite wide

117
10:57.150 --> 11:04.600
and then rebake, what you're going to do is find any narrow areas where it could get through before are

118
11:04.760 --> 11:05.440
now gone.

119
11:05.440 --> 11:11.800
So, for example, the agent can't get down this tunnel any more because it was really one of the narrowest

120
11:11.800 --> 11:13.000
sections

121
11:13.000 --> 11:18.730
and despite the fact that if you select this 'Agents' tab up here and have a look what's going on here,

122
11:19.030 --> 11:27.270
you can modify the size of any particular type of agent which we'll look at later on,

123
11:27.280 --> 11:32.540
it still has no effect whatsoever over this Baked Agent Size.

124
11:32.540 --> 11:41.800
So it's the Baked Agent Size that determines how the navmesh gets generated on the surface of your environment,

125
11:42.520 --> 11:50.410
NOT the size of the individual agents that you might create. You could have Humanoid, Zombie,

126
11:51.850 --> 11:58.960
Fish, whatever you want, you can have all different types of A.I. but they will not affect the baking of

127
11:58.960 --> 11:59.420
the navmesh,

128
11:59.650 --> 12:01.550
this will in here.

129
12:01.690 --> 12:13.430
So if we bring this back down to 59 and rebake it, we'll get our little path underneath this tunnel again.

130
12:13.710 --> 12:21.150
Even if I had a different agent in here and he was absolutely enormous and he was bigger than

131
12:21.480 --> 12:29.190
0.59 in radius, he would still be able to follow the path and he would work his way through the

132
12:29.190 --> 12:35.130
geometry, so probably not something you want and you CAN control that but not right here.

133
12:35.130 --> 12:39.180
The other thing you can set is the slope that the agent can walk down.

134
12:39.200 --> 12:48.960
So if I just move this over and have a look down here, in this lounge area there's a slope from this platform down

135
12:48.960 --> 12:57.600
into there and we can change this over in the Bake area. The 'Max Slope' that that agent's able to navigate

136
12:57.660 --> 12:59.730
is currently set to 45.

137
12:59.760 --> 13:06.630
If we said that this agent just can't use slopes whatsoever and set it to 0 and then rebake it,

138
13:07.020 --> 13:12.390
you'll see that there now is no path that the agent can take to get there.

139
13:12.420 --> 13:19.850
Now in taking this out, you might have also disturbed the path from where the agent currently is elsewhere

140
13:19.850 --> 13:26.570
and let's have a look over here. You can see that the path over this section here is now gone as well.

141
13:26.570 --> 13:36.140
Now even if I press Play at this point, I can click legitimately in these areas but the agent can't get there.

142
13:36.140 --> 13:38.230
So if I click out here, he can move to there

143
13:38.360 --> 13:40.130
but if I try and get him to come in to here,

144
13:40.130 --> 13:45.390
it's going to follow the path as far as it can until it gets stuck.

145
13:45.390 --> 13:47.960
Now you can see in this case what's happened, it's over

146
13:47.980 --> 13:55.220
here now, is because it tried to calculate the shortest path to get to here and because it couldn't go

147
13:55.220 --> 14:01.100
this way, it went all the way around the back and then it got as close as it possibly could to there

148
14:01.130 --> 14:02.390
but couldn't reach it.

149
14:02.390 --> 14:06.070
So if we click over here, the agent can come over here

150
14:06.080 --> 14:09.310
but, of course, it can't get through there because there's no path

151
14:09.380 --> 14:13.870
and if we click down this area here, again there's just no way to get there

152
14:13.880 --> 14:20.720
so it won't, even though it does try. Underneath, where you set the Maximum Slope, you can also set the

153
14:20.720 --> 14:21.660
Step Height

154
14:21.680 --> 14:25.940
and this is really great if you've got environment with lots of steps and you want your agents to get up

155
14:25.940 --> 14:28.580
on top of things. In this environment,

156
14:28.580 --> 14:32.330
there's not really any steps, but let me have a look around.

157
14:32.630 --> 14:34.410
There's a container here.

158
14:34.490 --> 14:39.770
Now this container, you can navigate on top of it

159
14:39.800 --> 14:44.250
if you're already on top of it. There's no path to get up on top of this container.

160
14:44.510 --> 14:53.390
If I select this container (and I'm just going to move it over to there, where we can see it clearly, let's get that box out 

161
14:53.390 --> 15:01.580
of the road) and then if we bake right now you'll see that it gets the top on it, but we still can't get

162
15:01.580 --> 15:03.030
up there. It's too high.

163
15:03.060 --> 15:06.240
So I'm going to move it down into the ground a bit,

164
15:06.230 --> 15:16.420
so maybe about...there and press [Bake]. Now when we press Play, we'll see if we can get up on top of it.

165
15:16.440 --> 15:25.620
So we'll go over there and the agent goes near it, but it can't get up on top of it. So it's close, but not quite.

166
15:25.920 --> 15:28.090
Let's just make that step a bit higher.

167
15:30.110 --> 15:33.470
You can't make a step higher than your agents.

168
15:33.470 --> 15:36.380
So Unity will stop you if you try and do it.

169
15:36.440 --> 15:36.680
Ok,

170
15:36.680 --> 15:44.660
now what you can see is that it has drawn up the side of the box here, which clearly shows us that the path

171
15:44.690 --> 15:46.050
for that step exists.

172
15:46.070 --> 15:54.220
So now when I press Play and I click on the box, he's going to pop up on top.

173
15:54.240 --> 15:59.570
So there it is there and then he will also be able to get off again as well.

174
15:59.570 --> 16:06.390
So it's like the jumping height, as well as for going up stairs. Just to finish up with

175
16:06.390 --> 16:07.100
this lecture,

176
16:07.100 --> 16:14.220
what I'm going to do is go back to the Bake and we will increase our slope again so that we can get

177
16:14.220 --> 16:21.450
inside of that area and bake that. Ok, it brings this path back here

178
16:21.630 --> 16:30.780
and then with Agent itself (our RedAgent) we can just Ctrl-D and make a whole bunch of copies of it.

179
16:30.810 --> 16:32.890
Now they're going to be all exactly the same.

180
16:32.930 --> 16:38.160
They're all going to have the same tag of 'ai' which means the AgentManager will pick them up and send through any

181
16:38.160 --> 16:43.600
click messages to them, which will set their destination. Now before they even move,

182
16:43.620 --> 16:49.680
their physics will not make them collide with each other and they won't use their own avoidance system to

183
16:49.680 --> 16:54.080
avoid each other either because we haven't triggered that to happen.

184
16:54.090 --> 17:01.800
But as soon as I click in here they will all pop apart and now you can see them all seemingly working

185
17:01.800 --> 17:04.170
together to get the same location.

186
17:04.170 --> 17:10.060
But all they're doing is just trying to get to where you've clicked whilst avoiding each other.

187
17:10.230 --> 17:14.510
So you can have as many of these little red guys as you like

188
17:14.520 --> 17:18.940
and the AgentManager will automatically pick them up for you.

189
17:22.180 --> 17:29.950
All right, so that is the basics of creating a single agent and some of the nuances of the map and setting

190
17:29.950 --> 17:37.240
up the navigatable areas on there. In the next lecture, we'll look at adding a different agent type so

191
17:37.240 --> 17:38.440
that we have 2

192
17:38.650 --> 17:41.550
and restricting where they can go.