Join Sue Blackman for an in-depth discussion in this video Scripting player input, part of Animating Characters with Mecanim in Unity 3D.
In this chapter, we'll be creating a script that manages the perimeters and tells Mecanim state engine when they've changed. Now, by the way is also a good time to start organizing our Mecanim assets. So, let's create a folder for them and name it Mecanim Assets. Create folder. And inside this folder, I want to create a couple more controllers and scripts. And then we'll start by creating a script in our new scripts folder, so I'm going to Right click again and we're going to be using Java script, and we'll name our first one Basic Character control.
Then I'll go ahead and double click it or you could also press Open in the Inspector to open it in the script editor. The first thing we'll need to access is the animator component, so we'll start with the variable to hold that. So I'll just head out to the snippets file for the code and I'll copy and paste my first line. Ctrl+C and then I'm going to paste it up near the top.
So this one is an internal var named animator, which is going to find the animator component. And that's what we're going to store there. In the start function, we'll go ahead and assign it. So I'll head out to my snipits file again and grab the next bit we need because you don't want to watch me sit and type. Ctrl+C for copy, and we're going to put that into our start function. And if you want to tidy up you're more than welcome to. So this one we're going to use get component and we're looking for the animator component and then we assign it to the animator variable. And since our first scripts will go directly on the character, we only need to locate just the component not the object itself. So let's get our simple navigation working first. For that we're going to need a couple of variables to hold the current values of both the horizontal and vertical inputs.
So I'll head out and grab those as well. Ctrl+C and then we'll paste them in here, Ctrl+V. And so now we've got h, which is going to be holding the horizontal input value, and v which is going to be holding the vertical input value and we'll go ahead and keep them public so that we can watch them in the Inspector.
Now we need to assign the input values. Remember they're going to range from minus 1 to 1, and this is done in the update function where it's updated every frame, so I'll head out and grab those lines. Ctrl+C and in the update function, give myself a little bit of space. Ctrl+V, and I'm going to Right Click and Indent.
So here's where we're using the get access horizontal, this is what we get for the player's input and a vertical is right here as well. So let's go ahead and save the script and drop it on each of our characters. Save. We'll get back to our editor. Then I'm just going to drag and drop it from the Project View onto each of the characters and the robot. If we click Play now, nothing will happen because we still need to tell the animator controller where the values are.
To do that, we're going to need to set the value of the parameters directly in the animator. We'll be doing that in a fixed update function, so the value is reported at a constant rate. I'm going to head out to my snippets file again and grab the last bit of code. Ctrl+C, and in the script editor, give myself a little bit of space after the update function, and Ctrl+V to paste that in.
So right now we're only going to be reporting the V input. But when we do that, we should be able to see it in the animator's parameter section. So we'll Save the script. Head back to the editor and see what happens. This time when I click Play and use the W or Up Arrow we should see the characters move forward and we'll also see the V input value change as I do that, so let's click Play. I'll press my up arrow, and they start walking forward. If I let up, it drops back down.
Now we don't see anything in the input value here, because I don't have a particular character selected. So I'm going to go ahead and select Lenny, and now we can see that. Activate my game view and click the Up arrow again, and let it go, and you can see that it's working as we expect. So I'm going to stop play mode. The walk feels awfully slow, so to speed it up since the clip's velocity controls the speed, I need to set it on the walk state itself. So I'm going to select the walk state and let's give it maybe 1.5. Now I've stopped Play mode but if you were to set this during Play mode, you'd also find out that it would be retained unlike setting a lot of parameters in the inspector when you're in Play mode.
Before I click Play and test this because we have changed the speed before, I'm going to do one other thing. That is, we can't turn the characters yet, but we can make a walk backward state out of what we've already got at hand. So let's right click over the walk state and select Copy. And right Click and Paste, now we've got something named walk zero. Let's rename that to Walk Backwards. And instead of loading a new clip, all we need to do is change the speed to a negative number.
Now, 1.5 might be a little fast for a backwards walk, so I'm going to use negative one. We'll create the transitions next. They use the V input parameter. So I'll right-click from idle, Make Transition. Right-click from backwards and make this transition from idle into walk backwards, this time we're still using v input, but guess what? It's a negative value, so now we want to use less than minus point one.
That puts us into backwards. To get back into idle, our transition is going to be V input greater than minus 0.01 again. We should also make sure that he can only go backwards when he's not in the race state. So, I'm going to add an extra parameter on my walk backwards at the plus. I'm going to make sure that he also has to be in race state 0.
In other words, not racing. So let's go ahead and click Play and Test. I'll be using my input keys. He's in idle now. I click the Up Arrow, they go forward. If I stop, they stop. If I click the Down Arrow, they go backwards. And I'll let up on my key and so we can see that everything's working. So let's stop. You're probably thinking it would be nice if the character turned his head occasionally to see where he's going while he's walking backwards. You'll be able to see how to do that when we get into masking. So in this video, we've seen not only how we can tie the animator controller into the scripting, but also how we can use a negative value to run a clip, literally backwards.
- 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