Organizing States into Sub-state Machines

Whenever the Animator area gets too cluttered, you can always think of organizing your Animation States into Sub-State Machines. In this recipe, we will use this technique to organize animation states for turning the character. Also, since the provided animation clips do not include Root Motion, we will use the opportunity to illustrate how to overcome the lack of Root Motion via script, using it to turn the character 45 degrees to the left and right.

Organizing States into Sub-state Machines

Getting ready

For this recipe, we have prepared a Unity Package named Turning, containing a basic scene that features an animated character. The package can be found inside the 1362_07_04 folder, along with animation clips called Swat@turn_right_45_degrees.fbx and Swat@turn_left.fbx.

How to do it...

To apply Root Motion via script, please follow these steps:

  1. Create a new project and import the Turning Unity Package. Then, from the Project view, open the mecanimPlayground level.
  2. Import the Swat@turn_right_45_degrees.fbx and Swat@turn_left.fbx files in the project.
  3. We need to configure our animation clips. Select the Swat@turn_left file from the Project view.
  4. Activate the Rig section. Change Animation Type to Humanoid, and Avatar Definition to Create From this Model. Confirm by clicking on Apply.
  5. Now, activate the Animations section. Select the turn_left clip (from the Clips list), click on the Clamp Range button to adjust the timeline, and check the Loop Time option. Under Root Transform Rotation, check Bake Into Pose, and navigate to Baked Upon (at Start) | Original. Under Root Transform Position (Y), check Bake Into Pose, and select Baked Upon (at Start) | Original. Under Root Transform Position (XZ), leave Bake Into Pose unchecked. Click on Apply to confirm the changes.
    How to do it...
  6. Repeat steps 4 and 5 for Swat@turning_right_45_degrees.
  7. From the Hierarchy view, select the MsLaser character. Then, from the Animator component in the Inspector view, open the MainCharacter controller.
  8. From the top-left corner of the Animator view, activate the Parameters section and use the + sign to create the two new Parameters (Boolean) named TurnLeft and TurnRight.
  9. Right-click on the gridded area. From the context menu, select Create Sub-State Machine. From the Inspector view, rename it Turn.
    How to do it...
  10. Double-click on the Turn sub-state machine. Right-click on the gridded area, select Create State | Empty, and add a new state. Rename it to Turn Left. Then, add another state named Turn Right.
  11. From the Inspector view, populate Turn Left with the turn_left motion clip. Then, populate Turn Right with turning_right_45_degrees.
    How to do it...
  12. Get out of the Turn sub-state machine back into the Base Layer. By right-clicking on each state and selecting the option Make Transition, create transitions between Move and Turn Left, and Move and Turn Right.
    How to do it...
  13. Enter the Turn sub-state machine. Then, create transitions from Turn Left and Turn Right into the Move state.
    How to do it...
  14. Select the arrow that goes form Turn Right to (Up) Base Layer. It will turn blue. From the Inspector view, uncheck the Has Exit Time option. Then, access the Conditions list, click the + sign to add a new condition, and set it as TurnRight and false.
    How to do it...
  15. Select the arrow that goes from (Up) Base Layer to Turn Right. From the Inspector view, uncheck the Has Exit Time option. Then, access the Conditions list, click the + sign to add a new condition, and set it as TurnRight and true.
  16. Repeat steps 14 and 15 with the arrows that go between (Up) Base Layer and Turn Left, using TurnLeft as a condition, this time.
  17. From the Hierarchy view, select the MsLaser character. Then, from the Inspector view, open the script from the BasicController component.
  18. Immediately after the if(controller.isGrounded){ line, add:
    if(Input.GetKey(KeyCode.Q)){
      anim.SetBool("TurnLeft", true);
      transform.Rotate(Vector3.up * (Time.deltaTime * -45.0f), Space.World);
    }  else {
      anim.SetBool("TurnLeft", false);
    }
    if(Input.GetKey(KeyCode.E)){
      anim.SetBool("TurnRight", true);
      transform.Rotate(Vector3.up * (Time.deltaTime * 45.0f), Space.World);
    } else {
      anim.SetBool("TurnRight", false);
    }
  19. Save your script. Then, select the MsLaser character and, from the Inspector view, access the Basic Controller component. Leave the Move Diagonally and Mouse Rotate options unchecked. Also, leave the Keyboard Rotate option checked. Finally, play the scene. You will be able to turn left and right by using the Q and E keys, respectively.

How it works...

As it should be clear from the recipe, the sub-state machines work in a similar way to groups or folders, allowing you to encapsulate a series of state machines into a single entity for easier reference. States from the sub-state machines can be transitioned from external states, in our case, the Move state, or even from different sub-state machines.

Regarding the character's rotation, we have overcome the lack of root motion by using the transform.Rotate(Vector3.up * (Time.deltaTime * -45.0f), Space.World); command to make the character actually turn around when the Q and E keys are being held down. This command was used in conjunction with animator.SetBool("TurnLeft", true);, which triggers the right animation clip.

..................Content has been hidden....................

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