As we've seen, navmesh agent movement and animation don't work well together by default. We can't edit or overide
the navmesh agent class to consider the animation of our character,
so we need to come up with another method.
Luckily Unity provides a helpful animation setup and some script for beginners.
We're going to add some tools provided by Unity onto our zombie character to get her animation and her
navmesh movement to talk to each other.
Now you don't need to know a lot about the animation system to do this as the stuff provided by Unity
has all the set up done for you.
So with this lecture, you'll need to download the StrafeSetPackage and drag-and-drop it into
your Asset folder.
Once it's imported, you'll get an 'Animations' folder in your Assets and this is going to have all the necessary
tools that
we need to animate Stevie, so that she can follow us around properly and the animations will look nice
and smooth.
If you want to use your own character instead, you need an FBX file and a character that is rigged
for the Humanoid system.
Now if you're not an animator, you can get a whole bunch of free characters from Mixamo.com.
So you go to Mixamo.com and you can log in with your Adobe account.
You don't need to have a paid account, so this is a free service and you can select characters.
You can also select animations (but you don't need any animations for this exercise)
and you can pick whichever character that you like and then click on [DOWNLOAD] once they've come
up and it will give you the option to download for Unity, which is just there and then you get your character
and drag-and-drop it into your Assets folder.
Then you can drag-and-drop it into your scene and then you can apply the exact same processes that we're
doing here
and also make them into a Nav Mesh Agent.
When you first bring your FBX file from Mixamo into Unity, just drag-and-drop it from the desktop or
wherever into your Assets folder and it's going to ask you to fix the normal, so select [Fix now].
If you don't, it will lose all the connections to the textures, which is something you don't want.
You're going to end up with a 'model component' and then a '.fbm folder' which has all of the textures
in it and then a 'materials folder' as well. So this one here is the one that's got all of your animations
and components and whatever attached to it. What you want to do is grab that and drag it into your scene
which will now make your character part of your game.
When you do that, something really odd with the materials will be going on and all of the Normals
will have turned things inside-out and make it look a bit strange. To fix that, go into the Materials
folder and select each of the materials (shift-select), go over to 'Shader' in the Inspector, 'Legacy Shaders' and select
one of the legacy shaders. I usually pick 'Bumped Diffuse' which is the top one. That will fix your shading
problem on the character.
Now there's 2 other things you need to do to get your character ready in the same state as our zombie
character that we've been using, in order for it to work with this set up.
So select the character itself in the Hierarchy, you'll find the 'Animator' in there and this is where you drag
that Locomotion controller in,
over here later on.
But what's important at this point is that you tick this 'Apply Root Motion' so that it'll be the animation
that drives your character around as it's moving.
The other thing you need to do is turn your character into a Humanoid. That happens in Assets, where your
character is selected, then over in the Inspector you'll find 3 tabs: Model, Rig and Animations. We want
the Rig tab
and then go to Animation Type and set that to 'Humanoid'
(by default, it will come in as 'Generic')
and then click [Apply]. Now that new character is ready to do exactly what we did with our zombie
model, so from here you can go and add the Nav Mesh Agent and you can add all of the other components.
So that's a really quick overview of how you get a model from Mixamo into here
and if you're not an animator, then Mixamo is a fantastic resource to have access to.
So we've got our character. Here's Stevie set up exactly the same way as she was in the previous lecture.
She's got an Animator on her which was controlling her walking animation that I already gave to you
and then the ChasePlayer script and the Nav Mesh Agent. What we're going to do now to change her behavior is
to modify the zombie here, in the Animator.
What you're looking for (in this new Animations folder you would have downloaded) is the 'Locomotion'
controller.
There's 2 in mine.
If you've got 2, there might be one with a '1' on the end.
Just ignore that.
In fact, I might just delete it to get rid of it.
We only want this one that's LocomotionController.
Now with that, let's select Stevie again, you want to drag-and-drop that into the spot on the
Animator Controller.
The final thing to get this to work is...if you have a look in your Assets folder, when you imported
the StrafeSetPackage, you'll also get a Scripts folder which will have some scripts in.
The one you want is the LocomotionScript.
So drag-and-drop that onto Stevie and that will allow the animation
system you've just added to talk to the navmesh, to work out where the character should be and make
sure that the speed at which she's moving is coordinated with the speed of the animation.
Now if you press Play and go for a walk (I'll walk backwards so you can see what's going on)
you can see that Stevie is now grounded. She's not sliding.
Sure, her
animations have changed because we've had to use the custom ones that unity have given us for this particular
package.
Notice how her navmesh cylinder
(the green one) is in quite a different position to her actual position because what's happening in that script
is that it is negotiating the position of the model and the Nav Mesh Agent and coming up with
an average in-between of where to put the models.
So it's going to look realistic while it's moving around and chasing you at the same time.
There's two other scripts that come with that StrafeSetPackage.
There's a 'ClickToMove' and a 'LookAt' script. The ClickToMove script will get Stevie to move to
the location that you've clicked on the navmesh, much like we've already done in the past.
Let's just have a look at that though.
So what we're going to have to do is turn off our first person controller and add another camera into
the scene. So, right-click in the Hierarchy and select Camera.
Make sure that's not childed to anything
and then you probably want to set your camera up to look at the Scene view there. With the camera selected,
GameObject > Align With View.
Now when we press Play, we'll be able to click around here and get Stevie to move. Now select Stevie and we're
going to turn off the ChasePlayer script because she's no longer going to be able to find us to chase and we're
not using it.
We're going to use this 'ClickToMove', so drag that over into the Inspector and attach that to your agent.
The ClickToMove script uses the main camera to project the ray out into the navmesh area
and in order to do that and find it, make sure you've got your camera set and tagged as MainCamera
because we've turned off the FPS controller
so the main camera will now be gone. We need to create a new one to make sure that it can be found using
that MainCamera tag.
OK now press Play and you'll be able to click around the environment and Stevie will move.
Now you can see that she has a number of different types of animations that she can use.
Can we go up there on the stairs? She can move sideways and she can also move forward and backwards
and also on the spot and she has an idle animation for when she stops.
So that's what you'll get in that set.
Now the final thing that you do have as well is this LookAt script.
It allows you to move your character's head to look where they're going, so it just makes
it even more realistic.
So grab hold of that look at and let's put it over here.
Drag-and-drop it onto Stevie.
The thing you need to set up in this LookAt is the head.
It needs to know where that joint in your character is.
So if we get Stevie and we drop down the Hierarchy, you're looking underneath 'Hips'.
So this is the rigging that's going on inside the character (the bones of the character). Look down through
Spine and then Neck and then Head. Get the Head and drag-and-drop it into the 'Head slot' on the LookAt script
and this will now control the character's head using the built in inverse kinematics system that's in
the animation.
So press Play and it's very subtle,
but you can see she does turn her head to the direction that she's going in.
All right, so now that you've got 1 Stevie set up, you can actually duplicate her if you want and have
2 or more.
Press Play and they will act like Nav Mesh Agents...well because they are Nav Mesh Agents. So they'll keep
their distance from each other, whilst trying to move and also use that animation set that was given
to us by Unity.
If you're interested in adding in your own animations and you have your own animations, you need a set
of 9 animations and they're called a Strafe Set.
So they're movement for a particular state of your agent (so say running) but there are 9 different
phases of running. So there's forwards, backwards, running on the spot and then side to side and then all
of the diagonal directions as well.
That's the entire strafe set. To find where that is set up,
if you go to the Animations folder and then double-click on our LocomotionController to open up your
Animator window.
All of this control that's going on is happening inside the 'Move' state.
If you double-click on that and open it up, you'll find that it's a blend tree.
It's quite complex and it's probably not a good idea to go mucking around in here unless you know about blend trees.
But if you do happen to have all those animations and you want to change the animations, then I would
suggest you don't change any of the 'settings', but the animations themselves are in here.
So this is like running 0 degrees, which would be forward, then you've got running at 90 degrees
and then you got diagonally forward, diagonally backwards...
So this is what is called the Strafe Set and that's where that name comes from.
If you have your own animations, you can drag-and-drop them into here to set them up for your character.
But these ones have all been created specially to work with this system, which makes it look very realistic.
So if you really do want to know more about this, you'll see a link come up on the screen to where this
set can be downloaded from, in the Unity documentation.
If you're an animator familiar with all this, then it will help you understand it on another level.
So that is setting up complex animations of a character and using the navmesh pathfinding and navigation
to make those things talk to each other and look coordinated.