WEBVTT

00:03.490 --> 00:10.450
Let's turn our existing waypoint system into one that uses a nav mesh instead and will do this to give

00:10.450 --> 00:17.710
a real broad coverage of the NAV system before we start to delve into it more deeper in other lectures.

00:17.980 --> 00:24.130
So starting with the code that you finished up with in the previous lecture where the tank was commanded

00:24.130 --> 00:30.610
by buttons to follow around these waypoints that we've placed, we're going to replace the A Star code

00:30.610 --> 00:34.690
that we've put in there by hand with the nav mesh instead.

00:35.140 --> 00:41.980
Now, to turn the environment into a nav mesh in unity, the first thing you need to do is find all

00:41.980 --> 00:46.270
of the bits and pieces that are going to be part of the nav mesh.

00:46.270 --> 00:48.670
And that's all of the models in this case.

00:48.670 --> 00:55.570
It's going to be all of this stuff that's under level art in the hierarchy, and that includes things

00:55.570 --> 01:03.010
like the ground, plain, concrete houses, everything that's included with this particular map.

01:03.010 --> 01:08.740
It's already been set up to use NAV measures, but the way you do it from scratch is you select the

01:08.740 --> 01:12.850
item that you want to be included in the nav mesh system.

01:12.850 --> 01:19.210
So in this case, we're selecting concrete, which is that bit of concrete around the tank over in the

01:19.210 --> 01:25.630
inspector, next to the name of the object under where it's got static and a little dropdown box.

01:25.630 --> 01:29.020
You drop that down and let me just make this a bit.

01:29.740 --> 01:30.340
Small.

01:30.340 --> 01:30.840
Okay.

01:30.850 --> 01:37.090
And then in there you're looking for navigation, static, and you want that to be turned on.

01:37.510 --> 01:45.430
And everything that is turned on for all of the measures will then be included in the nav mesh calculation.

01:45.430 --> 01:50.050
So with that on, you can then open up the navigation window.

01:50.440 --> 01:56.380
The navigation window can be found under window, air navigation and I usually have it sitting under

01:56.380 --> 01:59.350
the inspector because you're not really going to it very much.

01:59.350 --> 02:03.820
After you've set up your world, go to the bake tab.

02:03.820 --> 02:11.680
Now the bake tab is going to process the nav mesh for your terrain based on all of the objects that

02:11.680 --> 02:18.670
you've made navigation static, and you do that by clicking on the bake button.

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

02:23.900 --> 02:26.260
Now these are the areas that are walkable.

02:26.500 --> 02:28.060
I know that they're walkable.

02:28.060 --> 02:34.720
If you quickly just duck over to the Areas tab in navigation, you'll see that that color blue means

02:34.720 --> 02:39.940
that those polygons that's generated are where the agent is allowed to walk.

02:40.690 --> 02:44.860
And it's only set up at the moment for like walkable and not walkable.

02:44.860 --> 02:48.040
There's also a jump on in there by default as well.

02:48.040 --> 02:55.930
Now, these walkable and not walkable are all determined by the way that your agent is set up.

02:55.930 --> 02:58.270
For now, we're just going with the default.

02:58.600 --> 03:03.250
Now, the other part of a navigation mesh is the agent itself.

03:03.250 --> 03:10.330
And to define what is an agent and what knows about the nav mesh so that it can pass find around the

03:10.330 --> 03:16.450
nav mesh you need to add a navigation agent component to your NPCs.

03:16.450 --> 03:18.400
In this case it's the green tank.

03:18.400 --> 03:25.330
So if you select the green tank, then in the inspector ad component and we're going to look for nav

03:25.330 --> 03:34.600
mesh agent, add that on that will immediately make that tank adhere to that nav mesh so that it can

03:34.600 --> 03:37.120
use and find the nav mesh.

03:37.330 --> 03:43.660
Up until now we've been coding in all of the steering behaviors for our NPCs.

03:43.690 --> 03:51.040
Now that you've made the tank a nav mesh agent, it will be already programmed with movement and speed

03:51.040 --> 03:57.430
and angular speed acceleration and all of those things, and you'll find the settings for those in the

03:57.430 --> 04:01.210
component that you just added to the tank again.

04:01.210 --> 04:06.130
For now, we'll just leave these as the default, but feel free to have a look at what they are.

04:06.130 --> 04:09.310
So you've got speed, angular speed acceleration.

04:09.310 --> 04:14.770
You can also set obstacle avoidance of how far it should stay away from things as well.

04:14.770 --> 04:24.130
Now it's this nav mesh agent that we're going to grab with the script and then tell it where our waypoint

04:24.130 --> 04:24.880
is.

04:25.000 --> 04:28.630
So now we're going to rewrite our Follow Path script.

04:28.630 --> 04:34.600
So just open that up and we're now going to get rid of all of the code that we don't need anymore.

04:34.630 --> 04:39.670
So we don't need the transform goal and the speed and everything.

04:39.670 --> 04:45.640
So let's just delete all that and you'll be surprised at how little code you do actually need.

04:45.640 --> 04:49.870
We do need the waypoint manager and all of those things.

04:49.870 --> 04:55.090
We don't need our graph anymore because we're actually using the graph that gets created by the nav

04:55.090 --> 04:55.690
mesh.

04:55.990 --> 05:02.710
But we do need to get hold of the agent that is on the actual tank.

05:02.920 --> 05:11.200
So when you go up to the top using unity engine dot I in here so that we can grab that and then down

05:11.200 --> 05:16.960
here we will put nav mesh agent and call that agent.

05:18.250 --> 05:22.990
Then we want to get hold of the agent and we'll do that down in our start.

05:22.990 --> 05:26.670
So under here we can go.

05:27.310 --> 05:29.950
Agent Dot.

05:32.860 --> 05:36.250
Equals is not get component.

05:37.630 --> 05:39.910
NAV mesh agent.

05:41.620 --> 05:42.220
Okay.

05:42.220 --> 05:43.900
So we've done that now.

05:43.900 --> 05:46.300
We're not actually using G anymore for our graphs.

05:46.300 --> 05:47.770
We can also get rid of that.

05:47.770 --> 05:55.480
The only thing we really need is the positions of all of these objects for our buttons of where we want

05:55.480 --> 05:56.680
to go.

05:56.860 --> 05:57.370
Okay.

05:57.370 --> 06:01.660
So first of all, before we get to that, let's come down into our late update.

06:01.660 --> 06:05.020
So what code do we need to keep and get rid of in here?

06:05.020 --> 06:06.040
Well, guess what?

06:06.250 --> 06:11.290
It's all of it because the nav mesh is going to do that for us or the nav mesh agent.

06:11.500 --> 06:15.100
Now let's go and have a look at what's left inside of these buttons.

06:15.100 --> 06:19.660
Well, there's code that obviously we're not using because I've already deleted things for it.

06:19.660 --> 06:24.850
So let's just actually, I'm going to keep that line there because we need to know which way point we're

06:24.850 --> 06:26.920
going to at least the position of it.

06:26.980 --> 06:30.940
So let's get rid of that one, that one and that one.

06:31.810 --> 06:40.030
And under here we're going to say agent dot set destination and then we give it the position that we

06:40.030 --> 06:41.080
want to go to.

06:41.080 --> 06:49.510
So in this case it's going to be woops zero dot transform dot position.

06:49.510 --> 06:54.040
Now you don't have to use the little red waypoints that we created.

06:54.040 --> 06:59.560
That was for our waypoint system and they have a position on the map that's fine.

06:59.560 --> 07:04.570
You can actually set this to be any position on the map where you want your tank to go to.

07:04.570 --> 07:07.180
Okay, so that's the helipad.

07:07.180 --> 07:08.410
Go to button.

07:08.410 --> 07:15.030
Now, working with our agents at Destination as a challenge, you can fill out your other buttons.

07:15.060 --> 07:19.120
You might have more than I've got, but whatever's left there, fill those out.

07:19.120 --> 07:21.220
So pause the video now and when we come back.

07:21.220 --> 07:22.750
I'll continue that for you.

07:24.240 --> 07:25.010
Welcome back.

07:25.020 --> 07:26.270
How did you go with that?

07:26.280 --> 07:32.380
Well, we need this line and we're going to just replicate it down here and down there.

07:32.400 --> 07:34.790
Then we just changed the waypoint that we're going to.

07:34.800 --> 07:38.190
So one in this case and four down there.

07:38.370 --> 07:39.090
Okay, great.

07:39.090 --> 07:40.440
So just save that.

07:40.440 --> 07:47.940
And now we'll go back into Unity, find our tank, which is my tanks over here.

07:48.240 --> 07:50.610
But it doesn't really matter where it is.

07:50.610 --> 07:51.480
It's on the nav mesh.

07:51.480 --> 07:55.410
So let's press play and then we can try out these buttons.

07:55.410 --> 08:00.440
So if we go go to ruin off the tank goes to the ruin.

08:00.450 --> 08:05.880
Now what you're going to find is can I just zoom in a little bit more on here for you?

08:05.910 --> 08:09.570
Let's go to the helipad, have a look where the tank went.

08:09.570 --> 08:15.000
It's not following the waypoints anymore because we've taken our whole waypoint system out and see how

08:15.000 --> 08:16.920
much more accurate it is as well.

08:16.920 --> 08:24.180
So it's the nav mesh, calculating the path and if we just go to a navigation, actually might need

08:24.180 --> 08:26.070
to stop playing so we can go back to the scene.

08:26.070 --> 08:33.000
So when the tank came through from the ruin, you saw it duck through between these buildings.

08:33.000 --> 08:34.830
Now notice that there's blue through there.

08:34.830 --> 08:42.570
So our nav mesh agent is actually able to navigate through there without hitting these things here.

08:42.570 --> 08:46.260
So nav measures very, very powerful concept.

08:46.260 --> 08:52.350
They use the aStar algorithm underneath to navigate and all the codes pretty much there for you.

08:52.350 --> 08:58.330
So yeah, use it and you'll notice as you go through a lot of my other things where I've got characters

08:58.390 --> 09:02.970
that moving around in my games, I use the nav mesh system because I don't have the time to sit down

09:02.970 --> 09:07.350
and reprogram all these waypoint systems in and it's nice and accurate and fast.
