Join Sue Blackman for an in-depth discussion in this video Creating substate machines, part of Animating Characters with Mecanim in Unity 3D.
With all the current functionality in our test controller, our animator view has been getting pretty complex. So in this video, we'll see about tidying it up a bit, as we look into a few more state and transition options. I've opened up the character set-up scene, and I've selected the test controller. And yeah, it's starting to look like a spaghetti factory. So we're going to start with substate machines. These give us a way to compartmentalize sequences of states. A perfect example would be the race sequence, where the character goes from on your mark, to get set, to go. So, let's create a sub-state to store the racing start up states in. We need an empty spot near Idle, but close to the Run, so let's make some room.
And I'm going to right-click in the empty spot, and instead of create state, we're going to create sub-state machine. And that's what this guy looks like. It comes in as an elongated hexagon, and it's blue. And we'll name it Start race, and yeah, it's pretty vacant over there in the inspector. If we double click the new sub state machine, we're taken to its private area in the view where you will see its new default state, up.
And I'm going to drag it over so we can see it better. And you can see where we are in the animator hierarchy, by checking the top bar again. We're in Start race, and there's Base layer. You can get back to base layer either by clicking on the name there, or you can double click on the new place holder, Base layer, and it takes us back into the base layer. So let's head back to the start race and add a race related states. We're going to need one for on your mark, one for get set, and one for go. So I'll Right click, Create Empty, Right click, Create Empty, and we'll need one more.
And there we go. This one is on your mark. This one is get set. And this one, is go. For the animations, we're going to add BB on your mark, BB get set, and BB run.
Now before we start doing our transitions you're probably going to figure out that if you right click on the base layer we only get the copy and paste options, so let's go ahead and set up the rest of our transitions next. Make transition, make transition. I'll go ahead and set these up first. We can do this using the race state parameter we created in an earlier video. So the transition into get set is race state two.
The transition into go. Is raise state three. Now let's go back to the base layer and set up the transition into the start race sub machine. So I'm going to double click on my up layer, and we want to go from idol to on your mark when the race state equals one. So I'm going to make a transition. And we want it to go to on your mark, and then we select it and that's going to happen when race state equals one.
If we try to make a transition back we're going to get the same copy and delete, so let's Double Click on our race state again... And now our go needs to go back to the regular base layer. So I'm going to right click, make trasition. And it's going to have two options. We're either going to go to the idle or the run. Let's set up the run first. For the run condition to be true, we need to use exit time.
We're going to let the player have that exit time of that run clip to be able to get himself set and ready to hit run. So the first thing is exit time. The second condition is, if the player is set and ready to go, he's going to have pressed the Forward key, so we're also going to be looking for V input greater than .1. If he doesn't do that, if he's forgotten to press his Forward key, we're going to make a transition back into Idle, and that's going to have a similar transition In that we're going to wait for our exit time before we do anything, then we're also going to stipulate that the input is less than .1, so in other words, if the player forgot to press the forward key, this guy's going to be left standing at the gate.
So now we ought to check it out to see if it works. In case you've left the raised state up to something other than zero, let's make sure that we are at zero. And I am, so I'm good. And then we'll select one of the characters. I'm going to go for Lenny this time and click play. Right now he's in idle. As soon as I set race state to one, he drops into on your mark. When I set it to two he goes in to get set, when I type in three he's off and running but since I didn't press my forward key he's gone back to an idol so he's off standing somewhere here looking around; let's stop playback So, let's have a little fun with this.
Let's go ahead and speed up the run clip in the Go state to 2. So, he's going to be really fast off his mark. And, now this is when we need to be able to be interrupted. So, let's uncheck atomic on both of these exits. So in the run, go to run, we'll uncheck atomic. And in the go to idle, we'll also uncheck atomic, in case the player is ready to get going. So as soon as he's in state 3, we should be able to get him running. But I haven't figured out how to type in three and click fast enough to switch the focus to the game window, but it should work.
So now we've got a pretty good portion of the race functionality start sand we've managed to do so without adding much more clutter, and by now you're probably getting an idea of how complex the logic behind transitions can get.
- 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