In this lecture, we're going to use a navmesh to get a character to chase our player character around
in an environment. If you want to download the Zombie Chaser Starter file that comes with this lecture,
you can follow along.
Open up the ZombieChaser scene that's inside the file and you'll find this set up here. You've got a zombie
character that's in there and she's got animations on her already, so that you'll be able to see her
walking
and there's also a player character. If you press Play at this point,
you'll see that you've got a first person controller that's already put in there and that's the default
Standard Assets one that you can get with Unity and you've got the zombie. The animation is on, but there's
nothing there pushing the zombie forward, whatsoever.
So, let's get to work.
First of all, we need to set up our navmesh.
So over in the Hierarchy, you'll find 3 things. Stevie (our zombie), an
FPS controller (which is us) and then there's the warehouse model itself.
If you select the parent, you'll get everything.
So we'll just apply the navmesh to everything.
The first thing we need to do, in the Inspector with everything selected, we'll just go up to the
'Static' and we'll make it into a 'Navigation Static' and say [Yes, change children] so that
all these meshes will be included in the calculation.
Now open up your Navigation tab.
We're not going to change any of the default settings in here, so select [Bake]. The default settings
are set up for an average-sized character, which are usually around 2 meters high with half a meter radius
and a step size of 0.4 (which is ok in this case) and a slope of 45, but there's not really a lot of slopes going
on in this particular warehouse. There's a lot more steps than anything. With that bake performed, you can now
just have a little bit of a look around and see all the different places where that bake went to
and there's also (if we go along and just zoom down here) you can see that it went upstairs, over they can see
it went up the stairs.
So if you walk over here, you'll be able to get the zombie to follow you up the stairs, which is what
we want it to do.
Now let's add some code onto Stevie, our zombie to get her to follow us around.
So, first of all, we need to click on her in the Hierarchy, go into the Inspector and add in the
Nav Mesh Agent, to turn her into something that can follow you around. You can immediately see once you've
done that, the enclosing cylinder that you'll get around that character that's going to push her along and
stop her running into things.
Next, we need to write some code that will set the destination for our agent.
So I've already got it here.
It's a C# file called 'ChasePlayer'.
So create that C# file and then drag-and-drop it onto the zombie character.
Now just open that up and we'll have a look at the code that's in there.
So, first of all, we've got a link to our player because the zombie needs to know where the player is
at all times, so that he can keep adjusting its destination.
Next we'll create a link to the NavMeshAgent
that's attached to our A.I. character. Then in the Start,
we set up both of those things.
The player will be grabbed using FindWithTag("Player"), so we'll set the tag of our player character
to 'player' and then the agent, as we've done before, is just grabbing hold of the
NavMeshAgent attached to this character. Now because we're using the navmesh, all we need to do in the
Update is just to keep setting the destination for the agent to be the players position.
So it's 1 line of code and the navmesh is pretty much doing all the rest of the work for you.
Once you've added that, save it, go back into Unity and make sure you've got your code attached to Stevie,
so that's right down the bottom here.
Let's go over to our FPSController, which is our player character in this case and make sure you tag
it as 'Player'. The tag 'Player' with a capital 'P' already exists as a default in Unity, so just select
that. At this point, we can press Play and see the effect of our code.
Now when we start, immediately the zombie is right on top of our character and you can see it there. I'll just
walk away, turn around, she's moving quite fast
and when she approaches us, she ends up exactly on our position.
You can move with the W,A,S,D keys or the arrow keys.
So let's adjust her speed and the fact that she gets very close to us. Hit the Escape-key to release
control of your mouse.
Stop playing, then select Stevie in the Hierarchy. In the Nav
Mesh Agent,
we can change the Speed by putting that down to 1 and we can set the Stopping Distance.
It's currently set to 0 by default, but let's say we want her to be 2 meters away from us
when she stops, rather than being right on top of us. Let's press Play and have a look
at the effect of that.
So if I turn around, she's stopped.
She's not trying to follow us because she's within that stopping distance of 2. Now if I walk away,
she's going to slowly (at a speed of 1) walk towards us.
You'll also notice that we haven't done anything about the animations lining up with the movement.
Don't worry about that right at this moment.
We want to get the functionality going first.
Now while you're wandering around, you might notice some odd behavior is happening with the zombie.
Let me just show you, so let's play this.
What I'm going to do is walk behind her, over towards that green door.
I'll walk backwards, so that we can see what she does.
She's walking towards us...
but then I go over here...
She turns around and goes away.
Now we haven't programmed that type of behavior in and it's like she's suddenly lost interest and can't
see us anymore.
But that's not what's happened.
She's not that clever.
We haven't programmed that into her.
What she's trying to do is find another path to get to us from the navmesh
she's on
and the reason being is this...let me escape. I'm going to go over and turn the Navigation window on so you can see
the navmesh in the window. Looking down here,
you'll see there's a gap in the navmesh.
Now it's very, very small BUT it is still a gap and Stevie cannot get across that gap because
she doesn't believe that there is a path that she can follow to get from where she was there, to where
we are now standing.
Instead, she thinks her path is to go some other direction, which she is.
So she's found a path and you can tell she's found a path because of the dark areas that the A*
algorithm has highlighted for us, which was to go quite a long way.
So she's solved the problem of not being able to get over this little tiny gap by going the long
way round and going up-and-over
and now back to us.
So if we had to walk backwards and skip over that little crack again, she's going to go the long way
round again which is amusing behavior to watch, but probably not exactly what you want
in this case.
So we need to find a way to bridge this little gap here.
Now it's not a matter of remodeling or adding more models or anything into it.
It's just a matter of using what are called 'Off Mesh Links'. An Off Mesh Link is a link that allows an agent
to jump across gaps in meshes.
So we've got our 2 meshes here. Let's see if
I can select them. So that's that one and I'll shift-select the other one which will pick that one up
there. So that's these 2 pieces of floor. What I'm going to do with them is go over into the Navigation
window...
you want to go to the Object tab and you want to tick 'Generate OffMesh Links'. What that will do is link
these 2 planes together.
We come back to the Bake tab and then go down to where it says 'Generated Off Mesh Links'
and set up the 'Jump Distance'. It's currently set to zero,
so if you try and bake at this point, you won't get any links made across those 2 meshes.
Let's say put in a value like 2 in here and press bake and you will see these circles pop up on
the mesh itself - I can zoom in for you
and just orientate around a bit and you'll see here that it sets up links so that the zombie will be able
to move across these meshes.
Let's press Play and try it out. Ok,
so I'm going to walk around and over here to the green door and she's going to follow and I'll go across
the gap and you can see that she skipped across that gap quite nicely and now she'll follow me
wherever there is that blue line.
Now the last thing I want to show you is another type of off mesh link but one where there is a drop
as well as a gap.
So what we've already done is create the one over near the green door, that bridges that little tiny
gap,
but I want Stevie to be able to follow me off the top of this particular container, which I can currently
get to,
but she can't. There's no linkages there to get up. So we can make a linkage easily by increasing the
step size that she can go up and if we set it to 1.6 and then bake
it and Stevie I'll also speed up to 5.
Let's just press Play and we'll be able to see that she's going to follow me up quite fast onto that
container.
So I'll just jump up here
and she's just there and you can see if I move back a bit, she's up on that container because of the step
size that allows her to get up there.
Though if I jump off and we look back she's going to have to go the other way to get back to me.
Now if you've watched The Walking Dead, you know zombies are pretty stupid and if you went off the edge of
a cliff, so would they!
So let's modify this navmesh so that it will allow agents to go from a certain drop height.
So to do that we're going to select where we're dropping from and also where we're dropping to (it doesn't
matter which order you select them in).
So shift-select those two parts there that we want to link with an 'off mesh link' and then we'll
go over into Navigation, under Object we want to click the 'Generate OffMesh Links' for these particular two boxes
then go back into Bake
and this time we change the 'Drop Height'.
So let's make it 10 because it's a
fair distance to drop down and then click on [Bake].
What you'll find is you get a series of circles appear that are linked (if I can just turn around so that you
can see it a bit better). There they are.
It's like a waterfall effect, where you can see that circles up here are linked to circles
down here.
These are the positions where Stevie will be able to jump off and follow us down from that container.
So press Play and we'll try this out.
So we want to go around here, up the stairs...
up onto this container
and...where is she?
Here she comes.
Ok, now when I jump off and turn around, she has followed us down there rather than going the long
way back again.
Ok, so that is how to get a character to follow you but also how to generate your 'off mesh links' which
are really important when you've got little gaps and funny things happening in your navmesh that
you want your agents to be able to navigate over and around.