Join Sue Blackman for an in-depth discussion in this video Controlling states with speed, part of Animating Characters with Mecanim in Unity 3D.
While using input key values as controllers is probably the most common means of managing a character's locomotion states, it's by no means the only method. In this video, we'll take a first look at using speed to control the state transitions. Let's start by loading a new scene. Speed test, unity package. So I'm going to right click, Import > Custom Package.
Go out to my Exercise Assets folder, and select Speed Test, and Import. So now, we've got a new scene called Speed Test, and let's go ahead and double-click again. We don't need to save this one. And it looks pretty much the same. But if we check the characters, we'll see that instead of the basic character controller, we've got the accelerator script and we are also using a speed control for our animator controller.
Lets take a quick look at that. So we're going to start out looking at regular transitions here. Just a simple transition from idle to walk to run eh, o a faster run. But before we hook up the animator. Let's go ahead and see what the movement our script is giving it before we hook them together. I'll click play, and we're going to have to click to start the action. And you can see, it starts slow And it gets faster and faster. And then it's going to stop when it hits a certain speed threshold. So now we need to go ahead and send the speed value from the script back to the animator controller.
So I'm going to open up the accelerator script. Head over to my snippets, and grab the line I want. Ctrl+copy An we need to add this one, in the fixed update, after the speed has been, updated. An I'm going to Ctrl+V to paste. So let's look an see what this thing is actually doing. So we've got a variable name moving, an that's what tells it to go ahead an move.
So as soon as we press the left mouse button, the fire one, moving value is set to true. When it's true, each time it goes through the fixed update, our speed is going to increase by 0.03. And, just like we were rotating the characters, this time we're going to translate them or move them using the speed we've set up there, time dot delta time, which is what breaks it into a particular time per second, And it's moving in the z or forward direction. Now if the speed, which we've been incrementing here is greater than ten then we're going to set moving to false, and that's what stops them, and when all that has been calculated now we're going to tell our animator controller what the current value of speed is.
So let's go ahead and save... Head back to the editor, and we'll go ahead and click Play again. And this time, watch the speed relative to each other. Before, remember, they went off at the same speed, and kept going at the same speed? Now, if I click Play, you can see that the longer legged characters quickly Outstrip the shorter leg ones, and they even go off the ground. So let's stop and figure out what happened.
There's actually two things going on here. The first is that in the animator controller, remember that parameter called "apply route motion"? Let's take a look at it. This guy right here, that is what tells mechanim to move the character at the velocity of it's animation clip, and right now, that is being added to whatever speed our script gives it. Now we could uncheck Apply Root Motion for all of the characters. And I'll go ahead and do it, because it's pretty quick. So we're going to uncheck Apply Root Motion.
And if we click Play again, we can review part You can see that now they're not getting the extra speed, but it's pretty rough looking, isn't it? So we're going to do a couple of things, and the first is, rather then unchecking Apply Route Motion for every character you put in the scene, when they're going to be controlled by the script. There's something else we can do. Let's open up our accelorator script again, and we're going to change FixedUpdate to OnAnimatorMove. And then we'll save.
So it's basically going to behave the same as the fixed update, but it does a couple of other things for us, and I'll get back to my editor, and the first thing you'll notice is if you'll select a character, now, instead of a check box for apply root motion. That tells us that it's being handled by the script, and that's kind of a nice reminder, so if nothing else, it's worth using for that, and if we hit play again and click, you can see that it does the exact same thing as to apply root motion off.
So now lets see about doing something with those transitions because they are pretty rough. With conventional input access values, when the player is controlling the character, we were using one and minus one. And we just had to live with it. But since we're controlling the animations according to speed, we've got an ideal scenario for using speed in a blend tree, and it just so happens I've got one ready for you, so the first thing we'll need to do is right click on it and set it as the default, and let's go ahead and click on it and see what's in it. So what you can see is I've added an idle a walk and my two runs I've also increased the speed of that second run.
So this blend tree, instead of blending between clips that are at the same speed, is doing just the opposite. It's blending as the speed increases. Now, you'll also notice that the transitions are quite a bit different in this one. And this is a perfect time to use compute thresholds. And tell it to compute with speed. Now the only thing it didn't do on this one, is it didn't double time it. So, I'm going to go ahead and set that one to 11. But what it's done is it's gone out and it's looked for the velocity of the speeds on its own and matched it as the thresholds, so that's a pretty handy thing to have.
So now let's go ahead and click Play again. Pick in the view port And look at how smoothly the transitions are this time. Now, we can do something with the stop, but, it's going to be abrupt anyway because of the scripting. So now you can see the beauty of a speed control blend tree. The characters blend smoothly between the various animation clips. So now we've seen how to use speed to control blend trees, as well as using on animator move To block root motion from the animation clips.
- Importing characters and setting scale
- Setting up the controller
- Creating transitions
- Scripting the controls
- Adding layers and layer masks
- Handling nonhumanoid appendages
- Configuring blend trees
- Controlling states with speed
- Adding a multistate jump
- Targeting with IK (inverse kinematics)
- Setting up generic rigs
- Testing the character in the game