In this lecture, I will show you how to take your Finite State Machine created in the last series of lectures
with the tank and waypoints and transfer it onto a NavMesh.
The first thing you need to do is prepare your geometry in the scene for the NavMesh.
Grab hold of something that you want to be part of your NavMesh and over
in the Inspector, you'll see right up in the top corner is this little 'Static' and a dropdown box.
Let me just move this across here.
When you drop it down, you want to tick 'Navigation Static' for it.
Now that's the ground plane.
If you're using this model that I've supplied (or that Unity also supplied) then it will all be marked
ready for the NavMesh.
Let me have a look at the rocks.
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
Navigation Static.
So once you've done that with all the bits-and-pieces that are going to be part of your NavMesh,
NOT your character or NPC - you don't want them involved. The waypoints themselves, they're not part of
the NavMesh either.
So my little spheres that I used to represent where my waypoints are, they are NOT Navigation Static.
So just the geometry of the map.
Now once you've done that, you need to open up the Navigation window.
Ok, so setting these values and then baking is pretty much all we're going to do for setting this up.
The next thing is to make the tank a 'NavMesh Agent'.
So select your character (in this case, the tank) and in the Inspector, you want to Add Component and add
the 'Nav Mesh Agent'.
Now in order to have the agent know about the waypoints, we need to set the waypoints as a destination
for the Nav Mesh Agent that's attached to our tank.
Now if you remember back to the previous tutorial, we did a lot of the moving around in the Patrol script, which
is just here and you can see that I've already updated the part that we need and that is here.
So rather than doing the slerping and the translating manually, we just have to set the destination for
our 'agent' and give it the current waypoint.
So we leave the calculation for waypoints and distances up here but we just use the SetDestination
method and give it a position that's somewhere within the boundaries of where that NavMesh
and our red waypoints are. This thing that we call 'agent' in this script is the Nav Mesh Agent that's attached
to the tank.
In order for the Patrol script to access it, in the NPCBaseFSM script, I've added in
a 'public UnityEngine.AI.NavMeshAgent agent'. This is what we're using in the Patrol script and
then setting that up down in the OnStateEnter here, which just grabs hold of the Nav Mesh Agent that
we attached to the green tank and that's what's doing all of the work for us.
So if you save that code and then switch back into Unity and press Play, you'll see the whole thing in
action...
So all I've done is added 2 or 3 lines of code and set up the geometry in the scene to be
a NavMesh
and we've changed the behavior of this waypoint system completely.
Now this agent is a little bit relaxed,
as far as when he gets through here you'll see. When he gets to a waypoint, he kind of turns relaxed, if
he goes past a waypoint he reverses a little bit, it's quite smooth and fluid and it looks
a little bit too soft for a tank.
So what you can do is go over into the tank settings and look at things like changing the Speed, you
might change the Stopping Distance, the Acceleration, Auto Braking as well, you might turn that on or off,
Stopping Distance
we might put at 2,
let's turn up the Angular Speed to 200 and then have a play and see what it looks like and how it
affects the turning.
So this time he didn't get stuck back there.
It's got a much nicer way of turning now when he gets to the waypoint, see how he doesn't turn and move
at the same time?
So again, it really is up to you on the behavior of your particular NPC.
Lastly, if you want your tank to attack your player character, then it's a matter of going into the code
where we've got that set up all ready and in here, OnStateUpdate where we used to have the 'turning
towards the opponent' and then slerping and then translating,
all we do is change that to 'agent.SetDestination' and set the destination to the opponent, where the
opponent is our player character.
Remember, you don't want the player character to have anything to do with the NavMesh, so waypoints
aren't part of the NavMesh and the player character isn't part of the NavMesh.
They're locations ON the NavMesh. Does that make sense?
They've got to be within the NavMesh
otherwise your character and NPC's never going to be able to find them or reach them.
Ok, so with that in there, let's just play and have a look at the behavior now of our tank.
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
attack us.
He's got a lot better moves than he had before,
as far as he can slide around the corner! Now my tank has the advantage that I can go through geometry, it
doesn't have the physics on it, but he can't.
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.
Now this is all happening in the NavMesh. We haven't programmed any of this ourselves, so this is how
powerful the NavMesh system is and he's just going to keep trying to chase me, until I manage to outrun
him, at which point
(looks like I already have)
he's going to go back into the patrolling.
OK so that is the very, very basics of setting up a NavMesh with an existing waypoint system.