Let's turn our existing waypoint system into one that uses a nav mesh instead and will do this to give
a real broad coverage of the NAV system before we start to delve into it more deeper in other lectures.
So starting with the code that you finished up with in the previous lecture where the tank was commanded
by buttons to follow around these waypoints that we've placed, we're going to replace the A Star code
that we've put in there by hand with the nav mesh instead.
Now, to turn the environment into a nav mesh in unity, the first thing you need to do is find all
of the bits and pieces that are going to be part of the nav mesh.
And that's all of the models in this case.
It's going to be all of this stuff that's under level art in the hierarchy, and that includes things
like the ground, plain, concrete houses, everything that's included with this particular map.
It's already been set up to use NAV measures, but the way you do it from scratch is you select the
item that you want to be included in the nav mesh system.
So in this case, we're selecting concrete, which is that bit of concrete around the tank over in the
inspector, next to the name of the object under where it's got static and a little dropdown box.
You drop that down and let me just make this a bit.
Small.
Okay.
And then in there you're looking for navigation, static, and you want that to be turned on.
And everything that is turned on for all of the measures will then be included in the nav mesh calculation.
So with that on, you can then open up the navigation window.
The navigation window can be found under window, air navigation and I usually have it sitting under
the inspector because you're not really going to it very much.
After you've set up your world, go to the bake tab.
Now the bake tab is going to process the nav mesh for your terrain based on all of the objects that
you've made navigation static, and you do that by clicking on the bake button.
Once you've done that, you'll see that your map changes and you get all of these blue areas.
Now these are the areas that are walkable.
I know that they're walkable.
If you quickly just duck over to the Areas tab in navigation, you'll see that that color blue means
that those polygons that's generated are where the agent is allowed to walk.
And it's only set up at the moment for like walkable and not walkable.
There's also a jump on in there by default as well.
Now, these walkable and not walkable are all determined by the way that your agent is set up.
For now, we're just going with the default.
Now, the other part of a navigation mesh is the agent itself.
And to define what is an agent and what knows about the nav mesh so that it can pass find around the
nav mesh you need to add a navigation agent component to your NPCs.
In this case it's the green tank.
So if you select the green tank, then in the inspector ad component and we're going to look for nav
mesh agent, add that on that will immediately make that tank adhere to that nav mesh so that it can
use and find the nav mesh.
Up until now we've been coding in all of the steering behaviors for our NPCs.
Now that you've made the tank a nav mesh agent, it will be already programmed with movement and speed
and angular speed acceleration and all of those things, and you'll find the settings for those in the
component that you just added to the tank 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 speed, angular speed acceleration.
You can also set obstacle avoidance of how far it should stay away from things as well.
Now it's this nav mesh agent that we're going to grab with the script and then tell it where our waypoint
is.
So now we're going to rewrite our Follow Path script.
So just open that up and we're now going to get rid of all of the code that we don't need anymore.
So we don't need the transform goal and the speed and everything.
So let's just delete all that and you'll be surprised at how little code you do actually need.
We do need the waypoint manager and all of those things.
We don't need our graph anymore because we're actually using the graph that gets created by the nav
mesh.
But we do need to get hold of the agent that is on the actual tank.
So when you go up to the top using unity engine dot I in here so that we can grab that and then down
here we will put nav mesh agent and call that agent.
Then we want to get hold of the agent and we'll do that down in our start.
So under here we can go.
Agent Dot.
Equals is not get component.
NAV mesh agent.
Okay.
So we've done that now.
We're not actually using G anymore for our graphs.
We can also get rid of that.
The only thing we really need is the positions of all of these objects for our buttons of where we want
to go.
Okay.
So first of all, before we get to that, let's come down into our late update.
So what code do we need to keep and get rid of in here?
Well, guess what?
It's all of it because the nav mesh is going to do that for us or the nav mesh agent.
Now let's go and have a look at what's left inside of these buttons.
Well, there's code that obviously we're not using because I've already deleted things for it.
So let's just actually, I'm going to keep that line there because we need to know which way point we're
going to at least the position of it.
So let's get rid of that one, that one and that one.
And under here we're going to say agent dot set destination and then we give it the position that we
want to go to.
So in this case it's going to be woops zero dot transform dot position.
Now you don't have to use the little red waypoints that we created.
That was for our waypoint system and they have a position on the map that's fine.
You can actually set this to be any position on the map where you want your tank to go to.
Okay, so that's the helipad.
Go to button.
Now, working with our agents at Destination as a challenge, you can fill out your other buttons.
You might have more than I've got, but whatever's left there, fill those out.
So pause the video now and when we come back.
I'll continue that for you.
Welcome back.
How did you go with that?
Well, we need this line and we're going to just replicate it down here and down there.
Then we just changed the waypoint that we're going to.
So one in this case and four down there.
Okay, great.
So just save that.
And now we'll go back into Unity, find our tank, which is my tanks over here.
But it doesn't really matter where it is.
It's on the nav mesh.
So let's press play and then we can try out these buttons.
So if we go go to ruin off the tank goes to the ruin.
Now what you're going to find is can I just zoom in a little bit more on here for you?
Let's go to the helipad, have a look where the tank went.
It's not following the waypoints anymore because we've taken our whole waypoint system out and see how
much more accurate it is as well.
So it's the nav mesh, calculating the path and if we just go to a navigation, actually might need
to stop playing so we can go back to the scene.
So when the tank came through from the ruin, you saw it duck through between these buildings.
Now notice that there's blue through there.
So our nav mesh agent is actually able to navigate through there without hitting these things here.
So nav measures very, very powerful concept.
They use the aStar algorithm underneath to navigate and all the codes pretty much there for you.
So yeah, use it and you'll notice as you go through a lot of my other things where I've got characters
that moving around in my games, I use the nav mesh system because I don't have the time to sit down
and reprogram all these waypoint systems in and it's nice and accurate and fast.