WEBVTT

0
00:10.970 --> 00:17.600
Let's turn our existing waypoint system into one that uses a navmesh instead and we'll do this to

1
00:17.600 --> 00:24.250
give a real broad coverage of the navmesh system before we start to delve into it deeper in other

2
00:24.260 --> 00:25.540
lectures.

3
00:25.550 --> 00:31.520
So starting with the code that you finished up with in the previous lecture, where the tank was commanded

4
00:31.520 --> 00:37.910
by buttons to follow around these waypoints that we've placed, we're going to replace the A* code

5
00:38.030 --> 00:42.690
that we've put in there by hand with the navmesh instead.

6
00:42.710 --> 00:46.820
Now to turn environment into a navmesh in Unity,

7
00:46.820 --> 00:53.420
the first thing you need to do is find all of the bits-and-pieces that are going to be part of the navmesh

8
00:53.420 --> 00:59.320
and that's all of the models. In this case, it's going to be all of this stuff that's under LevelArt

9
00:59.930 --> 01:08.570
in the Hierarchy and that includes things like the ground plane, concrete houses, everything that's included.

10
01:09.200 --> 01:13.160
With this particular map it's already been set up to use navmeshes

11
01:13.250 --> 01:19.520
but the way you do it from scratch is you select the item that you want to be included in the

12
01:19.520 --> 01:20.270
navmesh system.

13
01:20.270 --> 01:26.600
So in this case, we're selecting 'Concrete', which is that bit of concrete around the tank. Over in the

14
01:26.600 --> 01:33.470
Inspector, next to the name of the object under where it's got static and a little drop-down box you drop

15
01:33.470 --> 01:34.210
that down

16
01:34.460 --> 01:37.750
and (let me just make this a bit smaller...

17
01:37.760 --> 01:38.440
ok)

18
01:38.450 --> 01:46.180
and then in there, you're looking for 'Navigation Static' and you want that to be turned ON and everything that

19
01:46.430 --> 01:52.850
that is turned on for, all of the meshes will then be included in the navmesh calculation.

20
01:53.060 --> 01:59.720
So with that on, you can then open up the Navigation window. The navigation window can be found under

21
01:59.720 --> 02:01.890
'Window > Navigation'.

22
02:02.140 --> 02:07.100
I usually have it sitting under the Inspector because you're not really going to it very much after

23
02:07.100 --> 02:09.390
you've set up your world.

24
02:09.470 --> 02:12.640
Now the tabs here, I'll explain in later lectures

25
02:12.650 --> 02:16.320
but for now we're just going to do a really quick demo of how this works.

26
02:16.440 --> 02:18.420
Go to the 'Bake' tab.

27
02:18.610 --> 02:26.540
Now the Bake tab is going to process the navmesh for your terrain based on all the objects that you've

28
02:26.540 --> 02:33.420
made 'navigation static' and you do that by clicking on the Bake button.

29
02:33.470 --> 02:38.870
Once you've done that, you'll see that your map changes and you get all of these blue areas. Now these

30
02:38.870 --> 02:41.150
are the areas that are 'walkable'.

31
02:41.240 --> 02:47.920
I know that they're walkable...if we quickly just duck over to the Areas tab in navigation, you'll see that

32
02:47.930 --> 02:55.730
that color blue means that those polygons that's generated are where the agent is allowed to walk and

33
02:55.730 --> 03:01.820
it's only set up at the moment for 'Walkable' or 'Not Walkable'. There's also a 'Jump' in there by default

34
03:01.850 --> 03:02.770
as well.

35
03:02.780 --> 03:10.600
Now these 'walkable' and 'not walkable' are all determined by the way that your agent is set up.

36
03:10.670 --> 03:13.260
For now we're just going with the default.

37
03:13.340 --> 03:21.650
Now the other part of a navigation mesh is the agent itself and to define what is an agent and what

38
03:21.650 --> 03:25.700
it knows about the navmesh, so that it can path-find around the navmesh,

39
03:25.820 --> 03:31.100
you need to add a navigation agent component to your NPCs.

40
03:31.190 --> 03:37.730
In this case, it's the green tank, so if you select the green tank then in the Inspector [Add Component]

41
03:38.150 --> 03:47.460
and we're going to look for 'Nav Mesh Agent'. Add that on. That will immediately make that tank adhere

42
03:47.600 --> 03:51.890
to that navmesh, so that it can use and find the navmesh.

43
03:52.010 --> 03:59.450
Up until now, we've been coding in all of the steering behaviors for our NPCs. Now that you've made the

44
03:59.450 --> 04:07.850
tank a navmesh agent, it will be already programmed with movement and speed and angular speed, acceleration

45
04:07.970 --> 04:13.640
and all of those things and you'll find the settings for those in the component that you just added to

46
04:13.640 --> 04:14.830
the tank.

47
04:15.110 --> 04:21.020
Again, for now, we'll just leave these as the default, but feel free to have a look at what they are. So you've got

48
04:21.020 --> 04:23.940
Speed, Angular Speed, Acceleration.

49
04:23.960 --> 04:29.540
You can also set Obstacle Avoidance of how far it should stay away from things as well.

50
04:29.540 --> 04:38.180
Now it's this Nav Mesh Agent that we're going to grab with the script and then tell it where our

51
04:38.180 --> 04:39.790
waypoint is.

52
04:39.800 --> 04:46.050
So now we're going to rewrite our FollowPath script, so just open that up.

53
04:46.100 --> 04:48.090
We're doing all of this by hand at the moment

54
04:48.110 --> 04:53.540
but now that navmesh system in Unity is going to take over for us, so a lot of the stuff that we've

55
04:53.540 --> 04:56.160
got going on in here, we don't need anymore.

56
04:56.270 --> 04:59.590
So first of all, we can get rid of our speeds

57
04:59.600 --> 05:06.050
and our goal. Then we can get rid of our node system that we've got and also the graph. We don't need that

58
05:06.070 --> 05:07.300
anymore either.

59
05:07.400 --> 05:12.890
I'm keeping the waypoint manager in here because it's got hold of our waypoints that we've placed

60
05:12.950 --> 05:21.110
on the map, though now that the navmesh is in control, the waypoints won't be used as paths between

61
05:21.110 --> 05:27.830
points but rather as a destination or a goal that you want to get to. The actual pathfinding is now

62
05:27.830 --> 05:33.110
done by the navmesh itself and we'll probably find that the tank will take different routes

63
05:33.110 --> 05:35.150
now that it's using that instead.

64
05:35.420 --> 05:41.050
So in the Start, we don't need a currentNode and we don't need our graph set up.

65
05:41.210 --> 05:41.950
We'll leave

66
05:42.010 --> 05:45.460
our GoToHeli and GoToRuin in there.

67
05:45.500 --> 05:49.530
I'm just going to comment this out because we'll need it as a reference in a moment

68
05:49.640 --> 05:51.490
and then in our LateUpdate,

69
05:51.590 --> 05:56.650
we don't need ANY of this code, so we'll just get rid of everything and empty it out completely.

70
05:56.780 --> 06:00.550
The navmesh agent will now be doing all of the work for our tank,

71
06:00.620 --> 06:02.900
so we need to grab hold of that. At the top

72
06:02.900 --> 06:09.300
I will declare a link to that agent. So we create a

73
06:09.360 --> 06:12.890
UnityEngine.AI.NavMeshAgent called 'agent'.

74
06:12.960 --> 06:20.280
And then in the Start, we'll use the GetComponent to grab hold of that. To set the goal for the agent,

75
06:20.280 --> 06:25.140
we'll do that inside of the 2 methods that we've written that are run by the buttons.

76
06:25.200 --> 06:27.750
So it's quite simple really.

77
06:27.760 --> 06:36.660
We grab hold of that agent, then '.SetDestination' and it's that destination that will be the location

78
06:36.750 --> 06:42.480
of the waypoint that we want to go to. Again, these aren't waypoints as such, they're just goal locations

79
06:42.570 --> 06:43.670
on the map.

80
06:43.830 --> 06:46.920
In this case, it's wps[4].

81
06:46.920 --> 06:57.880
So it's our red sphere that is in our array position 4 and we want the 'transform.position' because

82
06:57.880 --> 07:01.380
we're going to a position and that's what SetDestination needs.

83
07:01.510 --> 07:06.720
Now for the Ruin, we can just copy this code and place it in here

84
07:09.720 --> 07:17.310
and set the position to wps[0] so the destination that you place in to here just has to be

85
07:17.310 --> 07:25.320
a position and it can be another agent that's moving around on a map or a set location anywhere within

86
07:25.410 --> 07:27.000
that agent.

87
07:27.000 --> 07:27.290
Right.

88
07:27.330 --> 07:29.260
That's all you need to do.

89
07:29.310 --> 07:35.370
The agent code and the navmesh code does all the rest of the work for you, so save that and we'll

90
07:35.370 --> 07:38.030
go back to Unity. Press Play.

91
07:40.940 --> 07:45.530
Now without a destination, obviously the tank is not going to go anywhere, but let's try and get to the

92
07:45.530 --> 07:47.420
helipad, which is over here.

93
07:47.530 --> 07:48.850
Hit [HeliPad].

94
07:48.980 --> 07:54.620
You see the tank has turned around. The turning around, the speed that it's moving, it's all set on the

95
07:54.620 --> 07:58.010
tank in the navmesh agent.

96
07:58.240 --> 08:03.830
So now the navmesh agent's there, you'll notice that it also just took a short cut and didn't follow the

97
08:03.860 --> 08:09.410
actual waypoint paths that we've set up from the previous system because they're no longer necessary.

98
08:09.410 --> 08:15.100
So let's try and go to the Ruin and see which way it will go this time.

99
08:15.290 --> 08:16.760
So it's going to go back there.

100
08:16.770 --> 08:21.930
Looks like it's going to take a shortcut across the corner because it's calculating the shortest distance

101
08:21.930 --> 08:27.280
to get there and this is what it's figuring out that the navmesh is and you can see, well it's even taken

102
08:27.280 --> 08:33.210
a shortcut between the buildings and over to where the Ruin is. If we have a look

103
08:33.210 --> 08:43.950
in the scene and click on the Navigation tab, you'll see that the tank CAN move up between the buildings.

104
08:43.950 --> 08:51.480
So if the tank was too wide, that's when you can go back and set the size of the agent so that it won't

105
08:51.540 --> 08:57.570
fit through these particular gaps and look at all those little nuances and changes that you can make

106
08:57.960 --> 08:59.850
to it in the following lectures.

107
08:59.850 --> 09:05.760
This was just to show you what navmeshes can do and how easy they are to set up, just with the default

108
09:05.760 --> 09:06.620
settings.