Course contentsShow
← Unity Game Development
Lesson 192 of 8283. Physics and InteractionFree lesson

Implementing Physics-Based Crouching Mechanics

Students will dynamically adjust the character's collider dimensions to allow passage through tight spaces using physics-safe resizing.

In this lesson, you will learn how to shrink your character’s collision volume dynamically to navigate tight spaces while maintaining physical stability.

When a character enters a crouching state, you cannot simply hide the mesh; you must modify the CapsuleCollider itself to prevent the player from getting stuck or hovering above the floor. If you just scale the object, you risk causing physics glitches or "tunneling" into floor geometry. Instead, we manually adjust the height and center properties of the collider during a FixedUpdate cycle.

The Physics of Resizing

When you shrink a collider's height, Unity calculates the new size from the center of the object. If you don't adjust the center position, the bottom of the capsule will lift off the ground, causing the player to "float." To keep the character grounded, you must shift the collider's center downward by half of the amount you removed from the height.

Here is how you can implement a basic crouching transition:

private void SetCrouchState(bool isCrouching)
{
    float targetHeight = isCrouching ? 1.0f : 2.0f;
    float targetCenter = isCrouching ? -0.5f : 0.0f;

    // Apply the changes to the CapsuleCollider
    playerCollider.height = targetHeight;
    playerCollider.center = new Vector3(0, targetCenter, 0);
}

Safety Considerations

Because we are modifying physical dimensions, always ensure your collider transitions happen within FixedUpdate or immediately before the physics step. Furthermore, if you are using Continuous Collision Detection (which we covered in a previous lesson), Unity will better handle the rapid change in volume, preventing the player from clipping through walls during the transition.

If the character is in a tight space, you should also implement a "check" using Physics.Raycast or a small trigger volume above the player's head before standing up. This prevents the character from "popping" through geometry if they try to stand up while under a low ceiling.

Key Takeaway: To crouch safely, scale your CapsuleCollider height and compensate by shifting the collider center downward to keep the character anchored to the floor.