In this lecture I want to talk about animation and how it affects your translation or moving of your
characters. If you want to follow along,
download the animation starter project with this lecture and open it up. Inside you'll find two characters,
so I've got a zombie character and I've got the little lumberjack that we've used previously.
Press Play and you'll find that they do the turning and the slerping, moving forward until they get to your
goal cube, which you can also still drive around the scene.
Now the problem with their animations at the moment if you have a look, is that they're skating or slipping
along the ground.
Now that's because we have them moving too quickly.
So stop playing. The script that I've attached to each is MoveLocal and it's based on script that we've
looked at before, but I've exposed the Speed so that we can change it.
Let's change the speed for the lumberjack and for Jill the zombie down to 0.5. Ok, now press
Play again.
In the case of the lumberjack, he looks as though he's taking steps and propelling himself forward and
there's not a lot of slippage in there.
So he's walking cycle is quite simple, as far as one foot and the other. With Jill,
she's got a much more complicated walking cycle, where she's not walking in a straight line.
She's kind of slipping and skating her feet out from side to side and therefore inside of her animation.
She has a forward-propelling movement and it's us trying to move that model with our code that is conflicting
with her movements as well, that are already baked into her animation.
Now I don't want to go into the complexities of Unity animation because it's opening up a huge can of
worms, but I want to show you a couple of different things you can do to fix this problem.
First of all, let's look at Jill because the lumberjacks animations are fine.
So if you find that you can control your animated character, move it with your translations and get that aligned
nicely so that the speed of the animation and the speed at which you're moving him with your code are nicely
synchronized, then that's fine.
But if you end up with a complicated model like this zombie, for example, then you may need a different
way of propelling them forward.
Luckily, the humanoid animation system in Unity already can propel characters forward based on their
animation.
So let's just go to Jill and what we're going to do is untick MoveLocal.
We don't want that code on her. Then what we're going to do is drop down the Animator. The Animator has
the animation control of the character on it.
Now what you can do with a humanoid character is tick a box called Apply Root Motion.
What this will do is move your character in the world, as the animation moves the character.
If you press Play at the moment without any script on there, her animation is not moving her and that's why she's
slipping on the ground because in her actual animation file, she would be moving forward.
To see that, click on Jill and then over in the Inspector, tick on the Apply Root Motion box and then
we'll just zoom in on Jill over here as well so we can see her moving. Then press Play and now without any code,
the animation itself is moving her forward and see how much more natural it looks.
Her feet look like they're planted and she's moving, but her forward motion isn't linear so therefore
trying to drive it forward with a Translate is not going to work.
If you have this kind of forward motion in your character already within the animation, then you
should try and use that to your advantage. It'll make the movement of your character look so much more natural.
So to test out using the animations to propel Jill forward, first of all turn back on the MoveLocal script
that's on her.
Then open up that script and what you want to do is remove any of the code related to pushing the character
forward, which is indeed this transform
down here at the bottom. We're also testing for distance and will need to comment that out as well, otherwise
we're not doing anything when we've reached the goal. Save that and let's go back into Unity.
What we're going to do with the lumberjack is turn him off, because we're not doing this for him
and let's play Jill and we'll see now that her animations are driving her forward. She's still slipping
because of that slerp that's in there.
So that's also affecting it as well.
You can see that her movement is just so much more natural now. To get her to turn around with the animation
system itself,
you would also need a zombie 'turn around' animation associated with it. Now because she's not reaching
the goal (she's just going to circle round and round)
at this point if you had an Idle animation, that's when you would set the animation to go to Idle
and then she would obviously stop moving anywhere because we're only using the animation system to push
her forward.
So that is the difference between pushing her forward with the code and using what's in the animation
system to push things forward.
If you're interested in learning more about animation and this type of movement, then I do have another
course on mastering animation in Udemy that you might like to have a look at, which will show you these
differences and really get into the nuts-and-bolts of the animation system
but for this course, we don't really have the time.