Let's turn our existing waypoint system into one that uses a navmesh instead and we'll do this to
give a real broad coverage of the navmesh system before we start to delve into it 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* code
that we've put in there by hand with the navmesh instead.
Now to turn environment into a navmesh 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 navmesh
and that's all of the models. In this case, it's going to be all of this stuff that's under LevelArt
in the Hierarchy and that includes things like the ground plane, concrete houses, everything that's included.
With this particular map it's already been set up to use navmeshes
but the way you do it from scratch is you select the item that you want to be included in the
navmesh 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 drop-down box you drop
that down
and (let me just make this a bit smaller...
ok)
and then in there, you're looking for 'Navigation Static' and you want that to be turned ON and everything that
that is turned on for, all of the meshes will then be included in the navmesh calculation.
So with that on, you can then open up the Navigation window. The navigation window can be found under
'Window > Navigation'.
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.
Now the tabs here, I'll explain in later lectures
but for now we're just going to do a really quick demo of how this works.
Go to the 'Bake' tab.
Now the Bake tab is going to process the navmesh for your terrain based on all 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 we 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 'Walkable' or 'Not Walkable'. There's also a 'Jump' 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
it knows about the navmesh, so that it can path-find around the navmesh,
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 [Add Component]
and we're going to look for 'Nav Mesh Agent'. Add that on. That will immediately make that tank adhere
to that navmesh, so that it can use and find the navmesh.
Up until now, we've been coding in all of the steering behaviors for our NPCs. Now that you've made the
tank a navmesh 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 FollowPath script, so just open that up.
We're doing all of this by hand at the moment
but now that navmesh system in Unity is going to take over for us, so a lot of the stuff that we've
got going on in here, we don't need anymore.
So first of all, we can get rid of our speeds
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
anymore either.
I'm keeping the waypoint manager in here because it's got hold of our waypoints that we've placed
on the map, though now that the navmesh is in control, the waypoints won't be used as paths between
points but rather as a destination or a goal that you want to get to. The actual pathfinding is now
done by the navmesh itself and we'll probably find that the tank will take different routes
now that it's using that instead.
So in the Start, we don't need a currentNode and we don't need our graph set up.
We'll leave
our GoToHeli and GoToRuin in there.
I'm just going to comment this out because we'll need it as a reference in a moment
and then in our LateUpdate,
we don't need ANY of this code, so we'll just get rid of everything and empty it out completely.
The navmesh agent will now be doing all of the work for our tank,
so we need to grab hold of that. At the top
I will declare a link to that agent. So we create a
UnityEngine.AI.NavMeshAgent called 'agent'.
And then in the Start, we'll use the GetComponent to grab hold of that. To set the goal for the agent,
we'll do that inside of the 2 methods that we've written that are run by the buttons.
So it's quite simple really.
We grab hold of that agent, then '.SetDestination' and it's that destination that will be the location
of the waypoint that we want to go to. Again, these aren't waypoints as such, they're just goal locations
on the map.
In this case, it's wps[4].
So it's our red sphere that is in our array position 4 and we want the 'transform.position' because
we're going to a position and that's what SetDestination needs.
Now for the Ruin, we can just copy this code and place it in here
and set the position to wps[0] so the destination that you place in to here just has to be
a position and it can be another agent that's moving around on a map or a set location anywhere within
that agent.
Right.
That's all you need to do.
The agent code and the navmesh code does all the rest of the work for you, so save that and we'll
go back to Unity. Press Play.
Now without a destination, obviously the tank is not going to go anywhere, but let's try and get to the
helipad, which is over here.
Hit [HeliPad].
You see the tank has turned around. The turning around, the speed that it's moving, it's all set on the
tank in the navmesh agent.
So now the navmesh agent's there, you'll notice that it also just took a short cut and didn't follow the
actual waypoint paths that we've set up from the previous system because they're no longer necessary.
So let's try and go to the Ruin and see which way it will go this time.
So it's going to go back there.
Looks like it's going to take a shortcut across the corner because it's calculating the shortest distance
to get there and this is what it's figuring out that the navmesh is and you can see, well it's even taken
a shortcut between the buildings and over to where the Ruin is. If we have a look
in the scene and click on the Navigation tab, you'll see that the tank CAN move up between the buildings.
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
fit through these particular gaps and look at all those little nuances and changes that you can make
to it in the following lectures.
This was just to show you what navmeshes can do and how easy they are to set up, just with the default
settings.