How to do it...

To change the size of a collider while crouching, follow these steps:

  1. Import the character with Idle, WalkLeft, WalkForward, WalkRight, IdleCrouch, WalkLeftCrouch, WalkForwardCrouch, and WalkRightCrouch animations.
  2. Create a new Animator Controller.
  3. Right-click on the empty space of the controller and choose Create State | New From Blend Tree. We need two such states. Name one of them Idle and the second one Steering.
  4. Create three float parameters in the controller: Speed, Direction, and Crouch.
  1. Create two transitions:
    • Idle | Steering with one condition: Speed parameter value greater than 0.5. Has Exit Time should be set to false and Transition Duration set to around 0.2 seconds.
    • Steering | Idle with one condition: Speed parameter value less than 0.5. Has Exit Time should be set to false and Transition Duration set to around 0.2 seconds.
  1. Double-click on the Idle state. Set the Blend Tree Parameter to Crouch, add two Motion fields, and uncheck the Automate Thresholds option. Set the first Threshold to 0 and the second one to 1. Assign the Idle animation to the first Motion field and IdleCrouch to the second one, as shown in the following screenshot:
Idle and IdleCrouch animations blending with a Blend Tree
  1. Double-click on the empty space in the controller to get out of the Blend Tree settings.
  2. Double-click on the Steering Blend Tree.
  1. Set the Blend Tree Parameter to Crouch and add two new Blend Trees (instead of Motion fields). Uncheck Automate Thresholds and set the first one to 0 and the second one to 1. Click on the first added Blend Tree and set its Parameter to Direction. Add three Motion fields. Uncheck the Automate Thresholds option. Set the first Threshold to -45, the second one to 0, and the third one to 45. Assign the WalkLeft animation to the first motion field, the WalkForward animation to the second one, and the WalkRight animation to the third one. Do the same for the second added Blend Tree, but assign WalkLeftCrouch, WalkForwardCrouch, and WalkRightCrouch animations instead, as shown in the following screenshot:
Nested Blend Trees used for normal and crouch states
  1. Close the Animator Controller and assign it to our character's Animator component.
  2. Assign the RootMotionSteering.cs script (follow the Using root motion to steer a character recipe if necessary), Rigidbody component, and a Capsule Collider component to our character.
  3. Create a new script and call it Crouch.cs.
  4. In the Update() function of the script, we first check the height of the ceiling above our character's head. To do so, we use the Physics.Raycast() function. We cast a ray from a position 25 cm above our character's feet (to avoid hitting ground obstacles) and the ray has a length of 2 meters. If the ray hits anything, we set the cantStandUp variable to true. It is used later in the script:
        cantStandUp = Physics.Raycast(transform.position + 
        Vector3.up * 0.25f, Vector3.up, 2f);
  1. Next we check if player holds the C key or if our character cannot stand up. If so, we set the Crouch parameter to play the crouch animations. We also scale down the Capsule Collider attached to the character (we get the reference to this component in the Start() function and save it in the capsule variable). We check if the Capsule Collider isn't scaled down already with the capsule.height > 1.5f line. This is because we don't want to scale the capsule every frame (especially if it was already scaled down):
       if (Input.GetKey(KeyCode.C) || cantStandUp) 
       { 
           anim.SetFloat("Crouch", 1f, 0.25f, 
           Time.deltaTime); 
           if (capsule.height > 1.5f) 
           { 
               capsule.height = 1f; 
               capsule.center = new Vector3(0f, 0.5f, 0f); 
           } 
        }
  1. Next we scale back the capsule and stop playing the crouch animations if the player doesn't hold the C key and the cantStandUp variable is false. Again we check if the capsule was scaled down and only then do we scale it up again:
        else 
        { 
              anim.SetFloat("Crouch", 0f, 0.25f, 
              Time.deltaTime); 
              if (capsule.height < 1.5f) 
              { 
                  capsule.height = 2f  
capsule.center = new Vector3(0f, 1f, 0f);
} }
  1. Assign the script to the character and play the game to see the effect. Make sure your scaled down Capsule Collider fits under the obstacle you have on your scene. You may need to adjust the capsule.height and capsule.center values in the preceding script accordingly.
..................Content has been hidden....................

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