WEBVTT

0
00:11.340 --> 00:16.980
In this lecture, we're going to look at how to set up multiple navmeshes and assign different sized characters

1
00:16.980 --> 00:18.960
to different ones.

2
00:18.960 --> 00:25.670
This will allow you to specify where different types of characters can and can't move. To follow along,

3
00:25.690 --> 00:32.890
download the AgentSizesStarter file that comes with this lecture and open up the BridgeTest scene.

4
00:33.580 --> 00:39.980
In that scene, you'll find 2 characters. We've got a guard and we've got a girl

5
00:40.000 --> 00:46.690
and if you just rotate around, you'll see that we've also got a castle wall with different-sized holes

6
00:46.690 --> 00:46.930
in it.

7
00:46.930 --> 00:54.430
So what we'll do is get the girl (which is the smaller model) to go through this narrow opening and

8
00:54.610 --> 00:58.750
make sure that the guard cannot get through there because he's too big.

9
00:58.750 --> 01:04.190
The same for that bridge...she'll be small enough to go across the bridge but he won't.

10
01:04.450 --> 01:09.610
The first thing that you're going to have to do is set up these characters for use in Unity.

11
01:09.730 --> 01:15.990
These are characters I've brought in from Mixamo. I've fixed their materials but I haven't done anything else so

12
01:15.980 --> 01:20.350
we're going to go through the whole set-up for their animations.

13
01:20.380 --> 01:25.480
The first thing you'll need to do is select each model and set it to Humanoid.

14
01:25.480 --> 01:30.450
So I'm selecting Andromeda (which is the girl) and over in the Inspector,

15
01:30.580 --> 01:37.480
I want to go to 'Animation Type > Humanoid' and then press [Apply].

16
01:37.630 --> 01:45.130
Then you want to do the same thing for the castle guard...select it in your project and then create a Humanoid

17
01:45.130 --> 01:47.800
out of it and hit [Apply].

18
01:48.010 --> 01:51.540
The next thing to do is set them up with their strafe set.

19
01:51.550 --> 02:00.350
Now, this project already has the animations and the scripts for that strafe set included.

20
02:00.540 --> 02:06.940
Open up the Animations folder and this is where you'll find that 'Locomotion' animation controller that was created

21
02:06.940 --> 02:14.890
by Unity. Select the castle_guard and in the Inspector you'll find the Controller slot is currently empty.

22
02:14.890 --> 02:21.330
That's where you want to drag-and-drop this LocomotionController, into there.

23
02:21.430 --> 02:24.940
Do the same thing for Andromeda, as well.

24
02:24.940 --> 02:34.000
So just select her in the Hierarchy and drag-and-drop that LocomotionController on to her. Next, you want

25
02:34.000 --> 02:37.270
to add the ClickToMove script.

26
02:37.280 --> 02:44.110
So if we go into the Scripts folder, these are the same ones that come with the characters for the strafe set.

27
02:44.380 --> 02:51.310
If you select your character (so Andromeda in this case) we'll drag-and-drop ClickToMove on to her and

28
02:51.350 --> 02:53.430
we will also drop LocomotionSimpleAgent script.

29
02:53.430 --> 02:54.910
I won't worry about the LookAt script.

30
02:54.910 --> 03:00.790
You can set up LookAt if you want. Do the same thing for the castle_guard...we want to drag ClickToMove

31
03:00.790 --> 03:06.130
and the LocomotionSimpleAgent script onto there.

32
03:06.210 --> 03:13.860
Now that will automatically add the Nav Mesh Agent component on to each one because the LocomotionSimpleAgent and

33
03:13.860 --> 03:22.980
the ClickToMove require that. Now we also need to make a navmesh out of our map. So select the Map,

34
03:22.980 --> 03:32.270
then we want to set it to Navigation Static and all the children that are underneath it because that

35
03:32.270 --> 03:36.770
would include the ground and all that.

36
03:36.920 --> 03:39.530
I just noticed that the bridge itself isn't part of the map.

37
03:39.560 --> 03:48.440
I'm going to add that down into there and we can make that Navigation Static as well.

38
03:48.560 --> 03:56.760
So select the Map, go to the Navigation tab (open up that window if you don't have it) and we'll go to

39
03:56.760 --> 04:01.680
Bake and just [Bake] as it is by default.

40
04:01.700 --> 04:07.190
So you can see there that you get a navmesh that will cover everything and then there's a really

41
04:07.190 --> 04:10.190
narrow one that goes across that bridge.

42
04:10.340 --> 04:19.080
If you check all of the links, you'll find that there's enough of a step and touching where

43
04:19.080 --> 04:22.560
these meshes join together.

44
04:22.560 --> 04:25.410
So with that set up as the default,

45
04:25.440 --> 04:34.230
if we press Play and then click over here, we'll find that our agents go for a little walk to get to

46
04:34.230 --> 04:35.390
the other side.

47
04:38.670 --> 04:44.280
Now currently we haven't set different sizes for the different agents, they're just all the same default

48
04:44.400 --> 04:53.130
agent and that's all based on the Baked Agent Size that is in the Navigation.

49
04:53.130 --> 04:54.390
Now while I was just running it,

50
04:54.400 --> 04:58.620
you might have noticed a bit of 'jitteriness' in the characters movement.

51
04:58.620 --> 05:00.120
Let's take a closer look at that.

52
05:00.120 --> 05:06.860
First of all, let's turn off the castle_guard just so he's not there and we'll do this on Andromeda.

53
05:06.900 --> 05:14.940
So select Andromeda so that you can see her navmesh agent cylinder, as well as her and then we'll

54
05:14.940 --> 05:16.980
press Play

55
05:17.190 --> 05:26.360
and what's happening in that script is that it's trying to line up the animations with the cylinder

56
05:27.710 --> 05:35.270
and when it goes jittery, you'll see that she actually gets ahead of that cylinder with her animations...

57
05:43.330 --> 05:45.150
just there, you can see that happening.

58
05:45.340 --> 05:51.540
The reason why is because the animation is a faster running animation than the speed you're allowing that

59
05:51.580 --> 05:53.620
agent to move.

60
05:53.620 --> 06:01.810
So to fix that you've got to select the character in the Inspector and for its Nav Mesh Agent, increase

61
06:01.900 --> 06:05.020
its Speed and that will go a long way to fixing that

62
06:05.020 --> 06:08.830
jitteriness. So let's make her speed 8.

63
06:08.830 --> 06:16.360
We'll go back for the castle_guard and turn him back on and we'll change his speed also to 8 and that

64
06:16.360 --> 06:19.080
will make the movement for them a lot smoother.

65
06:22.830 --> 06:28.440
Now he can currently run across the bridge because we don't have any restrictions on the size.

66
06:28.530 --> 06:35.190
What we'll do now is we'll create a different type of agent and assign that to the castle_guard, so let's

67
06:35.190 --> 06:37.320
stop playing this.

68
06:37.320 --> 06:43.710
Then in the Navigation window under Agents, we'll create another agent type.

69
06:43.710 --> 06:45.620
We've got a Humanoid in there

70
06:45.640 --> 06:51.110
but click on '+' to create a new agent and we'll call this one Guard

71
06:54.200 --> 06:55.790
and then modify the size.

72
06:55.790 --> 07:00.480
So let's make him 1 round and let's set his height to

73
07:00.560 --> 07:08.990
3. The Step Height should still be OK because it's alright for the basic character (the Humanoid) which

74
07:08.990 --> 07:11.660
we'll leave on the Andromeda model.

75
07:11.780 --> 07:16.060
Now to assign this 'Guard' to the castle_guard,

76
07:16.130 --> 07:19.690
click on the castle_guard and go to the Inspector.

77
07:19.700 --> 07:23.000
Have a look at your Nav Nesh Agent component

78
07:23.000 --> 07:28.420
where you've got Agent Type and it's in there that you want to set that to 'Guard'.

79
07:29.120 --> 07:31.630
So that gives you a different type.

80
07:31.670 --> 07:37.940
Now at this point, if we press Play, we're going to get a common error that people have been getting so

81
07:37.990 --> 07:46.250
press Play and click over here and we get this error that says 'SetDestination can only be called on an active

82
07:46.310 --> 07:49.790
agent that has been placed on a NavMesh'.

83
07:49.990 --> 07:56.740
Which is true, in a sense that the navmesh in the Navigation

84
07:56.740 --> 08:07.840
(the one that you've baked) is only working on that default Humanoid, yet you've got another agent called

85
08:07.930 --> 08:16.230
'Guard' that YOU think is sitting on that same navmesh, but UNITY is saying "No, I don't know about it".

86
08:16.240 --> 08:25.450
So what we need to do is use some beta code that has been developed and it's not part of Unity yet, but

87
08:25.480 --> 08:28.400
you can get it and put it into Unity.

88
08:28.480 --> 08:33.230
So instead of using THIS navmesh generator, we're going to use a different one.

89
08:33.280 --> 08:40.190
What I want you to do is bring in the package that's called 'AINavMeshComponents' that came with this lecture

90
08:40.420 --> 08:46.740
and I'm going to just drag-and-drop that into the Assets folder.

91
08:46.990 --> 08:48.120
Now in there,

92
08:48.250 --> 08:54.970
there's a number of different scripts that we can apply. The one we're interested in is the one that's

93
08:54.970 --> 09:03.940
going to allow us to have multiple navmeshes on our map. So select the parent of the terrain (which in

94
09:03.940 --> 09:06.750
this case is our map) and go to the Inspector.

95
09:07.150 --> 09:15.100
We want to find the Scripts folder that came with our newly-imported NavMeshComponents which is

96
09:15.100 --> 09:22.600
just here and you're looking for the script called NavMeshSurface and that's the one we want to

97
09:22.600 --> 09:32.080
drag-and-drop onto our Map. We can also select [Add Component] and find it in this list as well.

98
09:32.170 --> 09:39.400
We actually need 2 of them, so we'll put another one on there. We want 1 surface for the Humanoid and

99
09:39.400 --> 09:44.090
1 surface for the Guard, so this top one we can leave as Humanoid

100
09:44.290 --> 09:49.440
and then the next one down, we can set that as Guard.

101
09:49.450 --> 09:57.890
Now if you click [Bake] for both of these, you'll get different navmeshes appear and you can see them in slightly

102
09:57.890 --> 09:59.070
different colors.

103
09:59.240 --> 10:05.270
So you've got one here and you've got one there and you've also got the old one hanging around from

104
10:05.270 --> 10:12.440
the default. If we just go back into the Navigation window and [Clear] that one, then it won't be a problem

105
10:12.440 --> 10:14.550
for us because we don't need it anymore.

106
10:14.600 --> 10:15.880
So back in the Inspector

107
10:16.130 --> 10:17.450
and we can see those two.

108
10:17.450 --> 10:24.950
Now what's happened here is that the water has been picked up as part of our navmesh.

109
10:24.950 --> 10:27.800
We don't want the water to be included.

110
10:27.830 --> 10:34.770
So over in our Nav Mesh Surface settings, we can go to 'Include Layers'

111
10:35.180 --> 10:43.610
and currently it's got everything, but we can go down and select 'Water' to untick it and ignore it and

112
10:43.610 --> 10:50.700
we can do that on both different types of navmeshes and then [Bake] both of them back in.

113
10:50.720 --> 10:54.900
Then you'll see the water is now excluded.

114
10:55.110 --> 11:02.700
So the Guard (castle_guard) will travel on one navmesh and the Humanoid (Andromeda) will travel on the other and you can

115
11:02.700 --> 11:13.420
see over the bridge that only Andromeda is able to travel because the Guards agent is too wide.

116
11:13.660 --> 11:14.510
So let's try this out.

117
11:14.520 --> 11:23.240
Let's press Play...and we'll find that if we click over here, we've got Andromeda going across the bridge

118
11:23.270 --> 11:25.130
and the Guard going through the water.

119
11:25.340 --> 11:34.200
The other thing that we've got as well (if you just zoom in to the castle itself) is that Andromeda

120
11:34.200 --> 11:38.790
can go underneath, through this little narrow bridge part

121
11:39.270 --> 11:42.410
and both of them can go through the wider one.

122
11:42.420 --> 11:47.640
So if I go back to the game and just click over here, you'll find that she'll go through the middle because

123
11:47.640 --> 11:53.760
she can fit, whereas he has to go around the outside but if we move them both over here and then ask

124
11:53.760 --> 11:55.010
them to come here,

125
11:55.230 --> 12:02.050
they'll both go through that bridge because they can fit.

126
12:02.250 --> 12:10.800
You can add as many different navmesh surfaces onto your terrain as you've got different types

127
12:10.910 --> 12:17.810
and this is the only way that you CAN have different types of agents. Even though the Navigation window

128
12:18.140 --> 12:26.030
allows you to have multiple agent types, unless you use those surfaces then the default navigation that

129
12:26.030 --> 12:33.500
you bake in, using this part here, is not going to find your other agent types and you'll get (as we saw)

130
12:33.590 --> 12:34.730
errors.

131
12:34.820 --> 12:38.800
So that's using multiple meshes and multiple agent sizes.