Building an animation state machine

Let's look at the Animators folder in the Project window. You will notice that although we created the animation clips, the folder also has a state machine. This will give us a leg up for the next step of the process:

  1. Double-click the mole state machine. The default state machine window, as shown in Figure 8. 5, will already have the MolePopUp animation clip attached to the Entry node:
Figure 8.5: Starting the state machine window
  1. Right-click in the grid space and select Create State | Empty.
  2. Rename the New State item Default in the Inspector panel.
  3. Create another new state called MolePopDown.
  4. Right-click the Default state and select Set as Layer Default State. This will change the start to Orange and the transition arrow will switch from MolePopUp to Default.
  5. Right-click the Default state again and choose Make transitions. Immediately click on the MolePopUp state to identify the transition point. A new arrow will appear between Default and MolePopUp.
  6. Repeat this process to create a transition from MolePopUp to MolePopDown and another from MolePopDown to MolePopUp.
  7. Click the Parameters tab in the upper-left side of the window.
  8. Create a Boolean variable by clicking the small (+) drop-down button in the upper-left corner of the Parameters tab and set its name to isActive.
  9. Create a second Boolean parameter called wasActive.

Select the MolePopUp state. The Inspector window should show that the Motion field value is MolePopUp. This was the clip we created in the Build an animator section. The following steps are optional and should only be followed if the Motion field is not set to MolePopUp:

  1. Click the selection target for the Motion field. A window will open with a list of animation clips.
  2. Since our project only has one clip, double-click MolePopUp to set the value.
  3. Select the MolePopDown state and set the MolePopUp motion field to the MolePopUp clip.
  4. Change the Speed attribute from 1 to -1. Setting the speed to a negative value will cause the clip to play at 100% speed in reverse.

The last steps for the state machine will tell our mole when to transition from popping up to popping down. We will do this in the Inspector panel for the transition arrows:

  1. Select the Default to MolePopUp arrow.
  2. Locate the Conditions section in the Inspector panel. Note that the list is empty.
  3. Click the (+) sign to add a condition to the transition.

Conditions are used by the state machine to determine when a transition should occur. Transitions can be assigned one, none, or multiple conditions. When assigned, all conditions in the list must be satisfied before the transition is triggered:

  1. Add the following conditions and values to the selected transition arrow:
    • Uncheck the Has Exit Time checkbox
    • isActive = true
    • wasActive = false
  1. Select the MolePopDown to MolePopUp transition arrow and set the following values:
    • Uncheck the Has Exit Time checkbox
    • isActive = true
    • wasActive = false
  1. Set the following conditions for the MolePopUp to MolePopDown transition arrow:
    • Uncheck the Has Exit Time checkbox
    • isActive = false
    • wasActive = true

Figure 8. 6 shows the completed state machine. Verify the transitional arrows conditions, and the Motion and Speed for each state:

Figure 8.6: Final state machine showing parameters and transition conditions

If you hit Scene Play now, our mole would not pop up. This is as expected, because our state machine is waiting for the isActive Boolean to get a TRUE value. We will add this functionality in the MoleController script.

  1. Save the scene.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.27.178