WEBVTT

0
00:10.920 --> 00:18.610
In this lecture, we're going to use a navmesh to get a character to chase our player character around

1
00:18.730 --> 00:24.510
in an environment. If you want to download the Zombie Chaser Starter file that comes with this lecture,

2
00:24.520 --> 00:26.000
you can follow along.

3
00:26.050 --> 00:33.490
Open up the ZombieChaser scene that's inside the file and you'll find this set up here. You've got a zombie

4
00:33.490 --> 00:39.610
character that's in there and she's got animations on her already, so that you'll be able to see her

5
00:39.610 --> 00:40.640
walking

6
00:40.690 --> 00:44.790
and there's also a player character. If you press Play at this point,

7
00:45.010 --> 00:49.900
you'll see that you've got a first person controller that's already put in there and that's the default

8
00:50.230 --> 00:57.800
Standard Assets one that you can get with Unity and you've got the zombie. The animation is on, but there's

9
00:57.800 --> 01:00.810
nothing there pushing the zombie forward, whatsoever.

10
01:00.860 --> 01:02.450
So, let's get to work.

11
01:02.450 --> 01:05.500
First of all, we need to set up our navmesh.

12
01:05.670 --> 01:11.860
So over in the Hierarchy, you'll find 3 things. Stevie (our zombie), an

13
01:11.890 --> 01:17.440
FPS controller (which is us) and then there's the warehouse model itself.

14
01:17.510 --> 01:21.410
If you select the parent, you'll get everything.

15
01:21.410 --> 01:25.010
So we'll just apply the navmesh to everything.

16
01:25.010 --> 01:30.500
The first thing we need to do, in the Inspector with everything selected, we'll just go up to the

17
01:30.500 --> 01:38.900
'Static' and we'll make it into a 'Navigation Static' and say [Yes, change children] so that

18
01:38.930 --> 01:42.660
all these meshes will be included in the calculation.

19
01:42.680 --> 01:45.170
Now open up your Navigation tab.

20
01:45.380 --> 01:52.220
We're not going to change any of the default settings in here, so select [Bake]. The default settings

21
01:52.590 --> 02:03.320
are set up for an average-sized character, which are usually around 2 meters high with half a meter radius

22
02:03.680 --> 02:10.910
and a step size of 0.4 (which is ok in this case) and a slope of 45, but there's not really a lot of slopes going

23
02:10.940 --> 02:17.270
on in this particular warehouse. There's a lot more steps than anything. With that bake performed, you can now

24
02:17.300 --> 02:23.470
just have a little bit of a look around and see all the different places where that bake went to

25
02:23.810 --> 02:31.490
and there's also (if we go along and just zoom down here) you can see that it went upstairs, over they can see

26
02:31.490 --> 02:33.320
it went up the stairs.

27
02:33.320 --> 02:39.620
So if you walk over here, you'll be able to get the zombie to follow you up the stairs, which is what

28
02:39.620 --> 02:40.970
we want it to do.

29
02:40.970 --> 02:49.190
Now let's add some code onto Stevie, our zombie to get her to follow us around.

30
02:49.190 --> 02:57.530
So, first of all, we need to click on her in the Hierarchy, go into the Inspector and add in the

31
02:57.680 --> 03:03.800
Nav Mesh Agent, to turn her into something that can follow you around. You can immediately see once you've

32
03:03.800 --> 03:11.570
done that, the enclosing cylinder that you'll get around that character that's going to push her along and

33
03:11.570 --> 03:13.400
stop her running into things.

34
03:13.400 --> 03:19.380
Next, we need to write some code that will set the destination for our agent.

35
03:19.400 --> 03:20.970
So I've already got it here.

36
03:21.200 --> 03:24.320
It's a C# file called 'ChasePlayer'.

37
03:24.320 --> 03:30.800
So create that C# file and then drag-and-drop it onto the zombie character.

38
03:30.800 --> 03:35.230
Now just open that up and we'll have a look at the code that's in there.

39
03:35.300 --> 03:43.130
So, first of all, we've got a link to our player because the zombie needs to know where the player is

40
03:43.220 --> 03:47.340
at all times, so that he can keep adjusting its destination.

41
03:47.360 --> 03:51.070
Next we'll create a link to the NavMeshAgent

42
03:51.080 --> 03:55.080
that's attached to our A.I. character. Then in the Start,

43
03:55.100 --> 03:56.750
we set up both of those things.

44
03:56.750 --> 04:06.150
The player will be grabbed using FindWithTag("Player"), so we'll set the tag of our player character

45
04:06.290 --> 04:13.470
to 'player' and then the agent, as we've done before, is just grabbing hold of the

46
04:13.500 --> 04:20.460
NavMeshAgent attached to this character. Now because we're using the navmesh, all we need to do in the

47
04:20.520 --> 04:27.480
Update is just to keep setting the destination for the agent to be the players position.

48
04:27.480 --> 04:33.170
So it's 1 line of code and the navmesh is pretty much doing all the rest of the work for you.

49
04:34.120 --> 04:41.020
Once you've added that, save it, go back into Unity and make sure you've got your code attached to Stevie,

50
04:41.040 --> 04:42.940
so that's right down the bottom here.

51
04:43.230 --> 04:50.190
Let's go over to our FPSController, which is our player character in this case and make sure you tag

52
04:50.190 --> 04:57.360
it as 'Player'. The tag 'Player' with a capital 'P' already exists as a default in Unity, so just select

53
04:57.720 --> 05:03.910
that. At this point, we can press Play and see the effect of our code.

54
05:03.960 --> 05:09.760
Now when we start, immediately the zombie is right on top of our character and you can see it there. I'll just

55
05:09.780 --> 05:14.690
walk away, turn around, she's moving quite fast

56
05:14.730 --> 05:19.620
and when she approaches us, she ends up exactly on our position.

57
05:19.620 --> 05:25.200
You can move with the W,A,S,D keys or the arrow keys.

58
05:25.330 --> 05:32.800
So let's adjust her speed and the fact that she gets very close to us. Hit the Escape-key to release

59
05:32.950 --> 05:34.500
control of your mouse.

60
05:34.510 --> 05:40.160
Stop playing, then select Stevie in the Hierarchy. In the Nav

61
05:40.210 --> 05:40.580
Mesh Agent,

62
05:40.610 --> 05:48.540
we can change the Speed by putting that down to 1 and we can set the Stopping Distance.

63
05:48.630 --> 05:54.750
It's currently set to 0 by default, but let's say we want her to be 2 meters away from us

64
05:54.750 --> 06:01.170
when she stops, rather than being right on top of us. Let's press Play and have a look

65
06:01.170 --> 06:02.730
at the effect of that.

66
06:03.120 --> 06:06.270
So if I turn around, she's stopped.

67
06:06.280 --> 06:12.470
She's not trying to follow us because she's within that stopping distance of 2. Now if I walk away,

68
06:13.600 --> 06:19.120
she's going to slowly (at a speed of 1) walk towards us.

69
06:19.190 --> 06:24.880
You'll also notice that we haven't done anything about the animations lining up with the movement.

70
06:24.890 --> 06:27.370
Don't worry about that right at this moment.

71
06:27.380 --> 06:30.310
We want to get the functionality going first.

72
06:30.620 --> 06:37.260
Now while you're wandering around, you might notice some odd behavior is happening with the zombie.

73
06:37.280 --> 06:39.810
Let me just show you, so let's play this.

74
06:40.130 --> 06:46.450
What I'm going to do is walk behind her, over towards that green door.

75
06:47.390 --> 06:50.160
I'll walk backwards, so that we can see what she does.

76
06:50.420 --> 06:52.450
She's walking towards us...

77
06:53.480 --> 06:56.220
but then I go over here...

78
06:56.790 --> 06:58.830
She turns around and goes away.

79
06:58.830 --> 07:04.770
Now we haven't programmed that type of behavior in and it's like she's suddenly lost interest and can't

80
07:04.770 --> 07:05.990
see us anymore.

81
07:06.120 --> 07:07.840
But that's not what's happened.

82
07:07.860 --> 07:09.000
She's not that clever.

83
07:09.000 --> 07:12.090
We haven't programmed that into her.

84
07:12.090 --> 07:17.730
What she's trying to do is find another path to get to us from the navmesh

85
07:17.880 --> 07:19.200
she's on

86
07:19.230 --> 07:26.250
and the reason being is this...let me escape. I'm going to go over and turn the Navigation window on so you can see

87
07:26.310 --> 07:29.680
the navmesh in the window. Looking down here,

88
07:29.700 --> 07:32.940
you'll see there's a gap in the navmesh.

89
07:33.120 --> 07:41.700
Now it's very, very small BUT it is still a gap and Stevie cannot get across that gap because

90
07:41.700 --> 07:48.300
she doesn't believe that there is a path that she can follow to get from where she was there, to where

91
07:48.300 --> 07:49.480
we are now standing.

92
07:49.650 --> 07:54.750
Instead, she thinks her path is to go some other direction, which she is.

93
07:54.750 --> 08:01.050
So she's found a path and you can tell she's found a path because of the dark areas that the A*

94
08:01.050 --> 08:06.470
algorithm has highlighted for us, which was to go quite a long way.

95
08:08.010 --> 08:13.110
So she's solved the problem of not being able to get over this little tiny gap by going the long

96
08:13.110 --> 08:15.410
way round and going up-and-over

97
08:15.480 --> 08:16.910
and now back to us.

98
08:17.010 --> 08:22.700
So if we had to walk backwards and skip over that little crack again, she's going to go the long way

99
08:22.700 --> 08:28.290
round again which is amusing behavior to watch, but probably not exactly what you want

100
08:28.290 --> 08:29.650
in this case.

101
08:29.730 --> 08:34.200
So we need to find a way to bridge this little gap here.

102
08:34.260 --> 08:40.470
Now it's not a matter of remodeling or adding more models or anything into it.

103
08:40.470 --> 08:51.810
It's just a matter of using what are called 'Off Mesh Links'. An Off Mesh Link is a link that allows an agent

104
08:51.810 --> 08:55.250
to jump across gaps in meshes.

105
08:55.290 --> 08:58.100
So we've got our 2 meshes here. Let's see if

106
08:58.170 --> 09:05.220
I can select them. So that's that one and I'll shift-select the other one which will pick that one up

107
09:05.240 --> 09:13.290
there. So that's these 2 pieces of floor. What I'm going to do with them is go over into the Navigation

108
09:13.290 --> 09:14.120
window...

109
09:14.400 --> 09:24.620
you want to go to the Object tab and you want to tick 'Generate OffMesh Links'. What that will do is link

110
09:24.950 --> 09:26.980
these 2 planes together.

111
09:27.230 --> 09:34.070
We come back to the Bake tab and then go down to where it says 'Generated Off Mesh Links'

112
09:34.070 --> 09:39.170
and set up the 'Jump Distance'. It's currently set to zero,

113
09:39.170 --> 09:44.730
so if you try and bake at this point, you won't get any links made across those 2 meshes.

114
09:44.930 --> 09:53.720
Let's say put in a value like 2 in here and press bake and you will see these circles pop up on

115
09:53.810 --> 09:56.460
the mesh itself - I can zoom in for you

116
09:56.750 --> 10:03.620
and just orientate around a bit and you'll see here that it sets up links so that the zombie will be able

117
10:03.620 --> 10:07.480
to move across these meshes.

118
10:07.490 --> 10:10.670
Let's press Play and try it out. Ok,

119
10:10.740 --> 10:19.150
so I'm going to walk around and over here to the green door and she's going to follow and I'll go across

120
10:19.150 --> 10:25.850
the gap and you can see that she skipped across that gap quite nicely and now she'll follow me

121
10:25.930 --> 10:30.170
wherever there is that blue line.

122
10:30.200 --> 10:38.390
Now the last thing I want to show you is another type of off mesh link but one where there is a drop

123
10:38.750 --> 10:40.150
as well as a gap.

124
10:40.400 --> 10:45.410
So what we've already done is create the one over near the green door, that bridges that little tiny

125
10:45.410 --> 10:45.740
gap,

126
10:45.740 --> 10:51.320
but I want Stevie to be able to follow me off the top of this particular container, which I can currently

127
10:51.320 --> 10:52.480
get to,

128
10:52.640 --> 11:00.020
but she can't. There's no linkages there to get up. So we can make a linkage easily by increasing the

129
11:00.020 --> 11:11.240
step size that she can go up and if we set it to 1.6 and then bake

130
11:11.240 --> 11:18.620
it and Stevie I'll also speed up to 5.

131
11:18.680 --> 11:29.630
Let's just press Play and we'll be able to see that she's going to follow me up quite fast onto that

132
11:29.630 --> 11:30.650
container.

133
11:34.390 --> 11:36.050
So I'll just jump up here

134
11:36.880 --> 11:42.380
and she's just there and you can see if I move back a bit, she's up on that container because of the step

135
11:42.380 --> 11:44.010
size that allows her to get up there.

136
11:44.080 --> 11:53.150
Though if I jump off and we look back she's going to have to go the other way to get back to me.

137
11:53.210 --> 11:58.490
Now if you've watched The Walking Dead, you know zombies are pretty stupid and if you went off the edge of

138
11:58.490 --> 12:00.460
a cliff, so would they!

139
12:00.470 --> 12:07.900
So let's modify this navmesh so that it will allow agents to go from a certain drop height.

140
12:07.970 --> 12:14.690
So to do that we're going to select where we're dropping from and also where we're dropping to (it doesn't

141
12:14.690 --> 12:16.160
matter which order you select them in).

142
12:16.160 --> 12:24.440
So shift-select those two parts there that we want to link with an 'off mesh link' and then we'll

143
12:24.440 --> 12:34.160
go over into Navigation, under Object we want to click the 'Generate OffMesh Links' for these particular two boxes

144
12:34.610 --> 12:37.190
then go back into Bake

145
12:37.550 --> 12:40.160
and this time we change the 'Drop Height'.

146
12:40.160 --> 12:42.330
So let's make it 10 because it's a

147
12:42.450 --> 12:46.340
fair distance to drop down and then click on [Bake].

148
12:46.610 --> 12:53.750
What you'll find is you get a series of circles appear that are linked (if I can just turn around so that you

149
12:53.750 --> 12:56.610
can see it a bit better). There they are.

150
12:56.770 --> 13:03.140
It's like a waterfall effect, where you can see that circles up here are linked to circles

151
13:03.410 --> 13:05.660
down here.

152
13:05.900 --> 13:13.340
These are the positions where Stevie will be able to jump off and follow us down from that container.

153
13:13.340 --> 13:16.710
So press Play and we'll try this out.

154
13:17.770 --> 13:21.130
So we want to go around here, up the stairs...

155
13:27.260 --> 13:28.950
up onto this container

156
13:30.590 --> 13:32.040
and...where is she?

157
13:32.050 --> 13:32.660
Here she comes.

158
13:37.210 --> 13:45.590
Ok, now when I jump off and turn around, she has followed us down there rather than going the long

159
13:45.590 --> 13:47.420
way back again.

160
13:49.180 --> 13:58.100
Ok, so that is how to get a character to follow you but also how to generate your 'off mesh links' which

161
13:58.100 --> 14:04.160
are really important when you've got little gaps and funny things happening in your navmesh that

162
14:04.160 --> 14:09.330
you want your agents to be able to navigate over and around.