WEBVTT

0
00:10.730 --> 00:18.470
We're going to continue looking at navmesh agents in this lecture by creating another agent that can

1
00:18.560 --> 00:21.330
only go on certain areas.

2
00:21.530 --> 00:26.620
So, first of all, grab hold of your RedAgent and just duplicate it.

3
00:26.630 --> 00:30.860
If you've got a whole bunch of other RedAgents because you were playing around with them from before,

4
00:31.100 --> 00:33.850
you can leave them there or you might want to get rid of them.

5
00:33.860 --> 00:40.820
Now I'm just going to press W here and move these agents apart and I'll use my blue material,

6
00:40.820 --> 00:44.460
a color that I made before and create the blue agent.

7
00:44.490 --> 00:54.810
So let's call this BlueAgent...and this BlueAgent, because it was a copy of the RedAgent, has the same

8
00:55.130 --> 00:58.950
AIControl script and the Nav Mesh Agent attached to it.

9
00:58.950 --> 00:59.620
They're important.

10
00:59.620 --> 01:05.970
You need to have those in there and we're not actually changing the code at all. We're changing the

11
01:05.980 --> 01:14.620
navmesh specification. What I want to do is make the BlueAgent unable to go down this corridor

12
01:14.820 --> 01:23.880
and that means modifying the areas within the navmesh so that the BlueAgent is unable to walk across

13
01:24.090 --> 01:25.980
this polygon that is here.

14
01:25.980 --> 01:33.270
So I'm going to go back into Navigation and we can see that everything is blue and it's all set to be

15
01:33.360 --> 01:34.130
walkable

16
01:34.140 --> 01:36.720
in this case, that's the first built-in value.

17
01:36.840 --> 01:42.890
We're going to add another one, let's call it Corridor. Now Corridor

18
01:42.900 --> 01:47.550
we want to assign to this area here.

19
01:47.550 --> 01:56.340
So you need to click on the polygon (or the mesh) that will be the corridor, so anywhere else you want

20
01:56.340 --> 01:57.750
to set to Corridor

21
01:57.750 --> 02:02.630
you can do at this time as well, but just by shift-selecting all of the parts that you want.

22
02:02.670 --> 02:03.880
So let's just do this one.

23
02:04.110 --> 02:14.740
Now that it's selected, go over into the Object tab under the Navigation tab. You'll see that this Navigation

24
02:14.740 --> 02:18.660
Static box is ticked because that's what we set it to

25
02:18.670 --> 02:21.590
back in the Inspector under Static here.

26
02:21.740 --> 02:29.950
Now if you select a polygon that you haven't baked before, such as this one here, you can

27
02:29.950 --> 02:33.410
select it as static here or in Navigation,

28
02:33.430 --> 02:37.810
you can actually tick it and this one is ticked as well.

29
02:37.810 --> 02:44.980
Ticking that does exactly the same thing as setting it

30
02:44.980 --> 02:47.140
Static in the Inspector.

31
02:47.140 --> 02:55.240
Now that we've created that corridor as an area for the navmesh, in this Object section we can set

32
02:55.660 --> 02:58.420
individual surfaces to be that type.

33
02:58.420 --> 03:06.160
So in this case, I can drop this box down and I'll find 'Corridor' because I typed that into the areas and so

34
03:06.160 --> 03:12.020
that polySurface4901 will become the corridor.

35
03:12.050 --> 03:21.300
If we go back into Bake and then bake it, you'll see that it goes pink and let's just check out the areas

36
03:21.330 --> 03:24.890
and you can see that the color for corridor is pink.

37
03:24.930 --> 03:30.390
Now you might've just noticed in here I've got this Height Mesh ticked because I was testing something out

38
03:30.390 --> 03:32.950
before. Let me just bake again without it.

39
03:33.060 --> 03:33.330
Right.

40
03:33.330 --> 03:37.200
So now we've done that, we've assigned that to the corridor.

41
03:37.200 --> 03:45.210
The next thing we need to do is tell the BlueAgent that it's unable to go down corridors.

42
03:45.450 --> 03:56.240
So we go back and find the BlueAgent which is here and in the Inspector at the bottom of its Nav Mesh Agent

43
03:56.340 --> 03:59.000
we don't change the Agent Type.

44
03:59.010 --> 04:06.780
We're still working with Humanoid but we do change the Area Mask which is right down the bottom and

45
04:06.870 --> 04:15.120
in here it shows you all the different parts of the navmesh that will be navigatable by that agent

46
04:15.240 --> 04:18.150
and in this case the corridor isn't.

47
04:18.150 --> 04:23.870
So we just select it and that will turn corridor off.

48
04:24.000 --> 04:28.300
Now the BlueAgent cannot go down the corridor.

49
04:28.470 --> 04:30.170
So let's test this out.

50
04:30.180 --> 04:37.710
We haven't changed any code, we've just changed the navmesh and where the agent can go. Press Play and

51
04:37.710 --> 04:39.350
we've got our 2 agents here.

52
04:39.390 --> 04:41.590
They'll be able to wander around

53
04:42.030 --> 04:47.770
but if I select the inside area, the RedAgent will be able to get there

54
04:47.880 --> 04:54.570
but the BlueAgent is stopped at the door because there's no possible way for it to get across this

55
04:54.930 --> 04:55.800
corridor.

56
04:56.070 --> 05:02.280
You can Ctrl-D to create a few RedAgents and Ctrl-D to create a

57
05:02.280 --> 05:11.070
few BlueAgents and watch them bustle around and also hustle past each other to try and get through

58
05:11.070 --> 05:15.150
the door because the blue guys are going to get in the road of the red.

59
05:15.150 --> 05:17.560
In this case, they're blocking them completely.

60
05:17.580 --> 05:20.120
Try and jiggle them around a bit

61
05:24.350 --> 05:31.010
and now one guy got through. So they might even move just a little bit because they still do jitter around

62
05:31.010 --> 05:33.710
the spot that they're on and some of them will get through.

63
05:33.710 --> 05:38.780
But you can see in this the behavior of the red and the blue.

64
05:38.780 --> 05:46.610
Now if the blue needed to get inside, then we need to create another path for them. To create a path for

65
05:46.610 --> 05:47.920
the BlueAgents,

66
05:47.930 --> 05:54.730
I'm going to put a ramp over here.

67
05:54.740 --> 06:01.820
So what I've done is I've removed the barrels that were around here and I've also gotten rid of the

68
06:01.820 --> 06:08.330
railings. So there were a couple of railings here and also over on the other side and I've stretched

69
06:08.330 --> 06:09.180
it out

70
06:10.260 --> 06:17.490
and rotated it so it's just a stock standard Unity cube, made it bridge across that area.

71
06:17.490 --> 06:22.950
It's not absolutely perfect and there's a bit of a step there but we are allowing for stepping

72
06:22.980 --> 06:25.050
up in this navmesh.

73
06:25.110 --> 06:32.760
I've also reselected a lot of these are other areas in the map to make them into navmesh as

74
06:32.760 --> 06:36.010
well, so that by the time the agents get over the bridge,

75
06:36.010 --> 06:39.680
then they can find their way back into that other area.

76
06:39.690 --> 06:44.810
Now this cube is not attached to the rest of the mesh.

77
06:44.850 --> 06:47.050
It's by itself and that's fine.

78
06:47.040 --> 06:53.610
It doesn't matter if it's a part of a whole mesh or a single object, it can still be integrated into the navmesh.

79
06:53.610 --> 07:00.570
Now we select that particular cube in this case and we go over to our Navigation window and down to

80
07:00.630 --> 07:01.480
Object.

81
07:01.620 --> 07:06.110
We make it a Navigation Static, ticking it in there.

82
07:06.150 --> 07:12.100
We can also tick it in the Inspector and then we set it to 'Walkable', which is what we want it to be.

83
07:12.120 --> 07:19.380
We go back to Bake and then click on [Bake] and you'll see that you get a nice path going across there.

84
07:19.590 --> 07:26.440
So let's go back and run this. When I click in here,

85
07:26.580 --> 07:29.750
the red guys are going to go down the corridor because they can.

86
07:29.760 --> 07:35.520
The blue guys don't even try to go down the corridor, so it doesn't even come into their calculations as

87
07:35.520 --> 07:40.610
they head off across the back and then they'll come in and through

88
07:40.620 --> 07:46.040
this other corridor that's over here and down into this area where the red ones are.

89
07:46.410 --> 07:55.620
Now the red guys are going to go the shortest path (or the least cost) to get into that area.

90
07:55.620 --> 08:00.640
Let's just see if we move the red ones over here first, if we can get them to go across the bridge.

91
08:00.660 --> 08:08.700
I'm just realigning that and I'll set my camera up...Align With View, so we can see more of the station and

92
08:08.700 --> 08:16.110
then we press Play and I click over here and they'll all go over there because that is the shortest path to get

93
08:16.110 --> 08:16.910
to there.

94
08:17.250 --> 08:24.840
Then if I click there, they will all head off across the bridge and then once they're over here, it should

95
08:24.840 --> 08:27.930
be shorter for them to come inside this way.

96
08:27.950 --> 08:28.230
Yes.

97
08:28.260 --> 08:36.280
So they'll come in through there. So in this case, we made them go over there and then it was a shorter distance

98
08:36.280 --> 08:40.780
for them to come this way than to go back and across through that corridor.

99
08:40.900 --> 08:49.710
But if they're standing here, at the Start, then the corridor is the quickest way for them to get inside.

100
08:51.390 --> 08:59.520
How could we make the corridor be more costly for them to go across? Let's say the

101
08:59.520 --> 09:06.110
corridor is on fire and it would make it a better idea for them to go around that way, than

102
09:06.120 --> 09:07.970
to come through here.

103
09:08.160 --> 09:13.440
What you do is increase the cost of this part of the path

104
09:13.440 --> 09:21.360
and we can do that in the Navigation window. If we go into Areas, 'Corridor' is currently set to a cost of 1,

105
09:21.360 --> 09:27.780
but if we made that a cost of 10 to go along that corridor, it's going to make the calculation a little

106
09:27.780 --> 09:29.520
bit different for them.

107
09:29.520 --> 09:33.720
Once you've changed the cost, you'll need to go back through and rebake it,

108
09:33.720 --> 09:36.990
so those values get saved into the navmesh.

109
09:36.990 --> 09:37.190
Ok.

110
09:37.200 --> 09:42.820
So let's press Play and see what we get now. So let's try and get them to go here.

111
09:44.690 --> 09:51.110
Now in this case, that must be very borderline that cost of 10 for them to come down because you can see

112
09:51.140 --> 09:58.700
2 of them who are close enough in vicinity go across through here, got there first, whereas the ones

113
09:58.700 --> 10:06.980
that were pushed by the blue guys further away from the corridor decided that it was easier to go around

114
10:07.280 --> 10:08.270
that way.

115
10:08.270 --> 10:14.630
So in this lecture, we've looked at creating different areas and assigning different areas to different

116
10:14.780 --> 10:26.180
objects that are in our navmesh and then assigning the different areas into the agent's own pathfinding

117
10:26.180 --> 10:31.920
system, which is in the navmesh agent by turning them on or off.