How to do it...

To use root motion for climbing, follow these steps:

  1. Import the character with Idle, WalkLeft, WalkForward, WalkRight, ToClimb, ClimbIdle, ClimbUp, and ClimbEnd animations.
  2. Create a new Animator Controller identical to the one from Using root motion to steer a character recipe (with Idle and Steering states).
  3. Add the Rigidbody component to the character and freeze its rotations.
  4. Add the Capsule Collider component to the character and make sure its Height and Center properties are set correctly (the Capsule Collider starts at your character's feet).
  5. Attach the RootMotionSteering.cs script to the character. It should now move (make sure to have a collider on the ground).
  6. Drag and drop the ToClimb, ClimbIdle, ClimbUp, and ClimbEnd animations into the controller.
  7. Create a bool Climb parameter and a bool ClimbUp parameter in the controller.
  1. Create seven additional transitions (see the following screenshot):
    • Idle | ToClimb with one condition: Climb parameter set to true. Has Exit Time should be set to false and Transition Duration set to around 0.1 seconds.
    • Steering | ToClimb with one condition: Climb parameter set to true. Has Exit Time should be set to false and Transition Duration set to around 0.1 seconds.
    • ToClimb | ClimbIdle with no conditions: Has Exit Time should be set to true and Transition Duration set to 0.1 seconds.
    • ClimbIdle | ClimbUp with one condition: ClimbUp parameter set to true. Has Exit Time should be set to false and Transition Duration set to around 0.1 seconds.
    • ClimbUp | ClimbIdle with one condition: ClimbUp parameter set to false. Has Exit Time should be set to true and Transition Duration set to around 0.1 seconds.
    • ClimbUp | ClimbEnd with one condition: Climb parameter set to false. Has Exit Time should be set to true and Transition Duration set to around 0.1 seconds.
    • ClimbEnd | Idle with no conditions: Has Exit Time should be set to true and Transition Duration set to 0.1 seconds.
Animator Controller for climbing
  1. Create two triggers (game objects with Box Collider components set to Is Trigger), ClimbStart and ClimbEnd, in the scene. One should be near the start of the ladder and the other near the end of it. Also create a RootTarget game object: it will be used to adjust the position of our character to match the ladder, as shown in the following screenshot:
RootTarget game object, ClimbStart, and ClimbEnd triggers placement
  1. Create a new Climb.cs script. Write a public void StartClimbing(Transform startNode) function in it. This function will be called from the ClimbStart trigger. In it, we turn off the rigid body physics, set the climbNode (the transform we want to adjust our position to), set the Climb parameter in the controller to start playing the ToClimb animation, and start an AdjustPosition coroutine to adjust our character's position:
        rb.isKinematic = true; 
        climbNode = startNode; 
        anim.SetBool("Climb", true); 
        StartCoroutine("AdjustPosition"); 
                
  1. Then write the IEnumerator AdjustPosition() co-routine in which we check if our character's position is close enough to the climbNode's position. If not, we adjust the position and rotation using the Lerp method. After we finish adjusting the position, we set a canClimb flag to true to turn on the climbing input in the Update() function:
        while((transform.position - 
        climbNode.position).magnitude > 0.05f) 
        { 
             yield return null; 
             transform.position = 
             Vector3.Lerp(transform.position, 
             climbNode.position, Time.deltaTime * lerpSpeed);           
             transform.rotation = 
             Quaternion.Lerp(transform.rotation, 
             climbNode.rotation, Time.deltaTime * lerpSpeed); 
         } 
          transform.position = climbNode.position; 
          transform.rotation = climbNode.rotation; 
          canClimb = true;            
  1. Next we write a public void EndClimbing() function that will be called from the ClimbStop trigger game object. In the function, we disable the climb input, set the Climb parameter to false in our controller, and start a ReEnableMovement coroutine to turn the rigid body physics back on:
        canClimb = false; 
        anim.SetBool("Climb", false); 
        StartCoroutine("ReEnableMovement");
  1. Our IEnumerator ReEnableMovement() coroutine waits a certain amount of seconds set by the public float climbEndAnimLenght variable. After that, it turns the rigid body physics back on:
        yield return new WaitForSeconds(climbEndAnimLenght); 
        rb.isKinematic = false;
  1. In the Update() function of the script, we handle the climb input:
        if (canClimb && Input.GetAxis("Vertical") > 0f) 
        { 
           anim.SetBool("ClimbUp", true); 
        } 
        else 
        { 
           anim.SetBool("ClimbUp", false); 
        }
  1. Assign the script to our character and create two more scripts (one for each of our triggers). Name them ClimbStartTrigger.cs and ClimbEndTrigger.cs. Both scripts have one just function: void OnTriggerEnter(Collider other).
  1. In the ClimbStartTrigger.cs script, we check if the entering object has the Player tag. If so, we get the Climb script component from that object and call the StartClimbing(Transform startNode) function on it. We pass a public Transform startNode object as the parameter. Our RootTarget game object is assigned to this variable:
        if (other.gameObject.CompareTag("Player")) 
        { 
           Climb climbScript = 
           other.gameObject.GetComponent<Climb>(); 
           climbScript.StartClimbing(startNode); 
        }
  1. In the ClimbEndTrigger.cs, we call the StopClimbing() function on the Climb script component:
        if (other.gameObject.CompareTag("Player")) 
        { 
            Climb climbScript = 
            other.gameObject.GetComponent<Climb>(); 
            climbScript.EndClimbing(); 
        }
  1. Assign the ClimbStartTrigger.cs script to the ClimbStart trigger game object and the ClimbEndTrigger.cs script to the ClimbEnd game object.
  2. Assign the RootTarget game object to the Start Node field of the ClimbStartTrigger script component in the ClimbStart game object's Inspector.
  3. Play the game and approach the ladder (make sure you enter the ClimbStart trigger). You may need to adjust the RootTarget, ClimbStart, and ClimbEnd positions and the climbEndAnimLenght variable's value to match your needs. You may also need to adjust the root movement in the ClimbUp animation and animation's import settings: mostly the Bake Into Pose options (for instance, you may need to set this option for your ClimbUp animation's Root Transform Position (XY) and Root Transform Rotation).
..................Content has been hidden....................

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