WEBVTT

0
00:08.720 --> 00:14.050
In this lecture, I will show you how to take your Finite State Machine created in the last series of lectures

1
00:14.060 --> 00:17.970
with the tank and waypoints and transfer it onto a NavMesh.

2
00:17.990 --> 00:23.930
The first thing you need to do is prepare your geometry in the scene for the NavMesh.

3
00:23.930 --> 00:31.100
Grab hold of something that you want to be part of your NavMesh and over

4
00:31.100 --> 00:36.620
in the Inspector, you'll see right up in the top corner is this little 'Static' and a dropdown box.

5
00:36.620 --> 00:38.870
Let me just move this across here.

6
00:38.930 --> 00:44.480
When you drop it down, you want to tick 'Navigation Static' for it.

7
00:44.540 --> 00:45.870
Now that's the ground plane.

8
00:45.920 --> 00:53.300
If you're using this model that I've supplied (or that Unity also supplied) then it will all be marked

9
00:53.300 --> 00:54.860
ready for the NavMesh.

10
00:54.860 --> 00:55.990
Let me have a look at the rocks.

11
00:56.000 --> 01:00.710
So just click a rock here and have a look at what it's marked as and you'll see it's also included as a

12
01:00.710 --> 01:03.290
Navigation Static.

13
01:03.310 --> 01:08.770
So once you've done that with all the bits-and-pieces that are going to be part of your NavMesh,

14
01:08.950 --> 01:15.550
NOT your character or NPC - you don't want them involved. The waypoints themselves, they're not part of

15
01:15.550 --> 01:16.620
the NavMesh either.

16
01:16.630 --> 01:22.810
So my little spheres that I used to represent where my waypoints are, they are NOT Navigation Static.

17
01:22.830 --> 01:26.500
So just the geometry of the map.

18
01:26.500 --> 01:30.500
Now once you've done that, you need to open up the Navigation window.

19
01:30.730 --> 01:37.070
Ok, so setting these values and then baking is pretty much all we're going to do for setting this up.

20
01:37.090 --> 01:42.200
The next thing is to make the tank a 'NavMesh Agent'.

21
01:42.430 --> 01:50.260
So select your character (in this case, the tank) and in the Inspector, you want to Add Component and add

22
01:50.350 --> 01:53.550
the 'Nav Mesh Agent'.

23
01:53.650 --> 02:03.490
Now in order to have the agent know about the waypoints, we need to set the waypoints as a destination

24
02:03.730 --> 02:08.680
for the Nav Mesh Agent that's attached to our tank.

25
02:08.680 --> 02:16.960
Now if you remember back to the previous tutorial, we did a lot of the moving around in the Patrol script, which

26
02:16.960 --> 02:23.660
is just here and you can see that I've already updated the part that we need and that is here.

27
02:23.950 --> 02:33.160
So rather than doing the slerping and the translating manually, we just have to set the destination for

28
02:33.160 --> 02:38.170
our 'agent' and give it the current waypoint.

29
02:38.290 --> 02:45.580
So we leave the calculation for waypoints and distances up here but we just use the SetDestination

30
02:45.580 --> 02:52.080
method and give it a position that's somewhere within the boundaries of where that NavMesh

31
02:52.120 --> 03:00.580
and our red waypoints are. This thing that we call 'agent' in this script is the Nav Mesh Agent that's attached

32
03:00.580 --> 03:01.930
to the tank.

33
03:01.930 --> 03:10.690
In order for the Patrol script to access it, in the NPCBaseFSM script, I've added in

34
03:10.720 --> 03:19.630
a 'public UnityEngine.AI.NavMeshAgent agent'. This is what we're using in the Patrol script and

35
03:19.630 --> 03:27.100
then setting that up down in the OnStateEnter here, which just grabs hold of the Nav Mesh Agent that

36
03:27.100 --> 03:32.610
we attached to the green tank and that's what's doing all of the work for us.

37
03:33.410 --> 03:42.380
So if you save that code and then switch back into Unity and press Play, you'll see the whole thing in

38
03:42.380 --> 03:48.830
action...

39
03:48.840 --> 03:56.850
So all I've done is added 2 or 3 lines of code and set up the geometry in the scene to be

40
03:56.850 --> 03:57.970
a NavMesh

41
03:58.170 --> 04:02.930
and we've changed the behavior of this waypoint system completely.

42
04:02.940 --> 04:06.420
Now this agent is a little bit relaxed,

43
04:06.450 --> 04:13.920
as far as when he gets through here you'll see. When he gets to a waypoint, he kind of turns relaxed, if

44
04:13.920 --> 04:21.630
he goes past a waypoint he reverses a little bit, it's quite smooth and fluid and it looks

45
04:21.630 --> 04:23.970
a little bit too soft for a tank.

46
04:23.970 --> 04:30.570
So what you can do is go over into the tank settings and look at things like changing the Speed, you

47
04:30.570 --> 04:38.370
might change the Stopping Distance, the Acceleration, Auto Braking as well, you might turn that on or off,

48
04:38.370 --> 04:39.150
Stopping Distance

49
04:39.150 --> 04:40.690
we might put at 2,

50
04:40.820 --> 04:49.770
let's turn up the Angular Speed to 200 and then have a play and see what it looks like and how it

51
04:49.780 --> 04:51.160
affects the turning.

52
04:52.260 --> 04:55.000
So this time he didn't get stuck back there.

53
04:56.370 --> 05:03.330
It's got a much nicer way of turning now when he gets to the waypoint, see how he doesn't turn and move

54
05:03.330 --> 05:04.780
at the same time?

55
05:07.450 --> 05:13.050
So again, it really is up to you on the behavior of your particular NPC.

56
05:13.060 --> 05:19.630
Lastly, if you want your tank to attack your player character, then it's a matter of going into the code

57
05:19.630 --> 05:27.820
where we've got that set up all ready and in here, OnStateUpdate where we used to have the 'turning

58
05:27.820 --> 05:31.840
towards the opponent' and then slerping and then translating,

59
05:31.840 --> 05:40.060
all we do is change that to 'agent.SetDestination' and set the destination to the opponent, where the

60
05:40.060 --> 05:42.570
opponent is our player character.

61
05:42.580 --> 05:48.550
Remember, you don't want the player character to have anything to do with the NavMesh, so waypoints

62
05:48.610 --> 05:53.050
aren't part of the NavMesh and the player character isn't part of the NavMesh.

63
05:53.050 --> 05:58.240
They're locations ON the NavMesh. Does that make sense?

64
05:58.280 --> 05:59.360
They've got to be within the NavMesh

65
05:59.380 --> 06:04.090
otherwise your character and NPC's never going to be able to find them or reach them.

66
06:04.090 --> 06:08.760
Ok, so with that in there, let's just play and have a look at the behavior now of our tank.

67
06:08.770 --> 06:14.680
So if we get close enough to our tank so we can trigger that, so he's now decided he's going to come and

68
06:14.680 --> 06:15.840
attack us.

69
06:18.420 --> 06:20.940
He's got a lot better moves than he had before,

70
06:20.970 --> 06:27.230
as far as he can slide around the corner! Now my tank has the advantage that I can go through geometry, it

71
06:27.280 --> 06:29.360
doesn't have the physics on it, but he can't.

72
06:29.370 --> 06:36.650
He's got to stick to the NavMesh, so you'll see that he'll try to find his own way back to attack me.

73
06:36.720 --> 06:42.390
Now this is all happening in the NavMesh. We haven't programmed any of this ourselves, so this is how

74
06:42.390 --> 06:49.860
powerful the NavMesh system is and he's just going to keep trying to chase me, until I manage to outrun

75
06:49.860 --> 06:51.090
him, at which point

76
06:51.210 --> 06:52.470
(looks like I already have)

77
06:52.470 --> 06:56.310
he's going to go back into the patrolling.

78
06:56.310 --> 07:03.900
OK so that is the very, very basics of setting up a NavMesh with an existing waypoint system.