How to do it...

To move a using animations, follow these steps:

  1. Import your character with root motion animations into Unity. We are using three animations in this example: Idle, WalkRoot, and RunRoot.
  2. Select the character asset file and go to the Inspector. Make sure the Animation Type is set properly for your character (Generic or Humanoid). If you are using a Generic character, make sure to set its Root Node.
  3. Go to the Animation tab and select the Idle animation. Select all the Bake Into Pose options. This bakes all the root motion data into the animation and makes it completely stationary. We don't want the Idle animation to move or rotate our character. You may also set the Base Upon option of the Root Transform Rotation to Original. This will make your character stand in the same pose as authored in the 3D software.
  4. For WalkRoot and RunRoot animations, select the same options as for the Idle animation, but uncheck Bake Into Pose for Root Transform Position (XZ). This will make our character move in the X and Z axes only. The Y movement of the root node and its rotation will be baked into the animation and will not be stored as root motion anymore.
  5. Set the Loop Time options to true for all animations.
  6. Apply the import settings.
  7. Place your character onto a scene.
  8. Select it in the Hierarchy and add a Capsule Collider to it. You may need to adjust the Capsule Collider properties to better fit your character. In our examples, we need to set the Height property to 2 units and the Y axis in the Center property to 1 unit.
  9. Add a Rigidbody component to the character and set its Constraints to Freeze Rotation in every axis.
  10. Navigate to the Animator component of the character (it is added automatically for animated game objects). Set Update Mode to Animate Physics. Make sure the Apply Root Motion option is checked in the Animator component.
  11. Create an Animator Controller asset for the character.
  12. Drag and drop the Idle animation into the Animator Controller to make it the default state.
  13. Create a float DesiredSpeed parameter in the Animator Controller.
  14. Right-click on the empty space in the Animator Controller and choose Create State | New From Blend Tree.
  1. Click on the Blend Tree and change its name in the Inspector to WalkAndRun.
  2. Double-click on the Blend Tree to open its settings.
  3. Click on the Plus button and choose the Add Motion Field option twice.
  4. Drag and drop your WalkRoot animation in the first (upper) field and your RunRoot animation in the second (lower) field.
  5. Uncheck the Automate Thresholds option.
  6. Set the WalkRoot animation Threshold to 1 and RunRoot animation Threshold to 2.
  7. Double-click on the empty space in the Animator Controller to get out of the Blend Tree settings.
  8. Create two transitions:
    • Idle | WalkAndRun with the condition set to DesiredSpeed parameter greater than 0.5, Has Exit Time set to false, and Transition Duration set to 0.2 seconds.
    • WalkAndRun | Idle with the condition set to DesiredSpeed parameter less than 0.5, Has Exit Time set to false, and Transition Duration set to 0.2 seconds.
  9. Write a script to set the DesiredSpeed parameter and assign it to the character. Play the game to see the effect (make sure your scene has a floor with a collider to prevent your character from falling down).
  10. You can also use the MoveAndSteer.cs script from the Scripts directory in this recipe. In the Update() function, we get the player input and save the values in hor and ver variables:
        hor = Input.GetAxis("Horizontal"); 

        ver = Input.GetAxis("Vertical"); 
  1. Then we use the Rotate() method to rotate our character in the global Y axis (Vector3.up) based on the hor variable and a public float rotationSpeed variable (set to 90 degrees per second):
        transform.Rotate(Vector3.up * hor * rotationSpeed * 
        Time.deltaTime); 
  1. We modify the desiredSpeed variable based on player input (we check if the player holds the Shift key):
        if (Input.GetKey(KeyCode.LeftShift) || 
        Input.GetKey(KeyCode.RightShift)) 
        { 
            desiredSpeed = 2f; 
        } 
        else 
        { 
            desiredSpeed = 1f; 
        } 
  1. Finally, we use the vertical axis input stored in the ver variable and our desiredSpeed value to set the DesiredSpeed parameter in the Animator Controller (which moves our character using root motion):
        anim.SetFloat("DesiredSpeed", desiredSpeed * ver, 0.2f, 
        Time.deltaTime); 
  1. We use the SetFloat() method with the dampTime parameter set to 0.2 seconds. That smooths out the changes of the parameter, which results in smoother blends between run and walk animations. The anim variable stores the reference to the Animator component and is set in the Start() function.
..................Content has been hidden....................

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