Crouching looks simple: press a key, make the player shorter. In practice you have to resize the collider, move the center so the feet stay on the ground, lower the camera smoothly, slow the movement and, most importantly, stop the player from standing up inside a low ceiling. This guide builds a complete Unity crouch with CharacterController, with a ceiling check, smooth transitions and a toggle or hold option, and shows what to change if you use a Rigidbody capsule instead.
How Crouching Works
A CharacterController is a capsule with three values that matter here: Height, Center and Radius. To crouch you reduce Height and move Center down by half the difference, so the bottom of the capsule stays where it was. If you only change Height, the capsule shrinks around its center and the player floats above the floor by half the change. That floating is the number one crouch bug.
Standing up is the dangerous direction. If something is above the player, growing the capsule pushes it into the ceiling and the controller can jitter or pop through. So before standing, you check whether there is room. If not, stay crouched.
If you do not have a movement script yet, build one from the first person controller tutorial first. The code below is a separate component that sits beside it.
Scene Setup
- A Player object with a CharacterController. A common standing setup is Height 2, Radius 0.5, Center (0, 1, 0), with the player’s pivot at the feet.
- A camera as a child of the Player, in the Hierarchy, at a local Y of about 1.7 (eye height).
- Walls, ceilings and the floor on layers you can mask. Put the player on its own layer, for example Player, so the ceiling check can ignore it. The collision layers matrix guide explains layers if you haven’t set them up.
The Crouch Script
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(CharacterController))]
public class PlayerCrouch : MonoBehaviour
{
[Header("Heights")]
[SerializeField] float standHeight = 2f;
[SerializeField] float crouchHeight = 1f;
[SerializeField] float heightLerpSpeed = 10f;
[Header("Camera")]
[SerializeField] Transform cameraHolder;
[SerializeField] float standEyeHeight = 1.7f;
[SerializeField] float crouchEyeHeight = 0.8f;
[Header("Input")]
[SerializeField] bool toggleMode = false;
[Header("Ceiling check")]
[SerializeField] LayerMask obstacleMask = ~0;
CharacterController controller;
bool wantsCrouch;
public bool IsCrouching { get; private set; }
void Awake()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
var kb = Keyboard.current;
if (kb == null) return;
if (toggleMode)
{
if (kb.leftCtrlKey.wasPressedThisFrame) wantsCrouch = !wantsCrouch;
}
else
{
wantsCrouch = kb.leftCtrlKey.isPressed;
}
// only stand up if there is room
bool shouldCrouch = wantsCrouch || (IsCrouching && !CanStand());
IsCrouching = shouldCrouch;
float targetHeight = IsCrouching ? crouchHeight : standHeight;
ResizeController(targetHeight);
MoveCamera();
}
void ResizeController(float targetHeight)
{
float newHeight = Mathf.Lerp(controller.height, targetHeight, heightLerpSpeed * Time.deltaTime);
if (Mathf.Abs(newHeight - targetHeight) < 0.001f) newHeight = targetHeight;
controller.height = newHeight;
// keep the feet on the ground: center is always half the height
controller.center = new Vector3(0f, newHeight * 0.5f, 0f);
}
void MoveCamera()
{
float eye = IsCrouching ? crouchEyeHeight : standEyeHeight;
Vector3 p = cameraHolder.localPosition;
p.y = Mathf.Lerp(p.y, eye, heightLerpSpeed * Time.deltaTime);
cameraHolder.localPosition = p;
}
}
This version reads the keyboard with the Input System package. Lerping the height gives a smooth squat. Setting controller.center to half the height works because the player’s pivot is at the feet; if your pivot is at the middle of the body, adjust accordingly. The shouldCrouch line is the key safety rule: even if you let go of the key, you stay crouched while CanStand is false.
The Ceiling Check
Add this method to the script. It checks whether a standing-height capsule would overlap anything.
bool CanStand()
{
float radius = controller.radius;
Vector3 feet = transform.position;
// capsule from just above the feet to full standing height
Vector3 bottom = feet + Vector3.up * (radius + controller.skinWidth);
Vector3 top = feet + Vector3.up * (standHeight - radius);
return !Physics.CheckCapsule(bottom, top, radius * 0.95f, obstacleMask,
QueryTriggerInteraction.Ignore);
}
Physics.CheckCapsule returns true if any collider overlaps the capsule. We invert it. Two details prevent false results. First, the radius is shrunk slightly (0.95f) so the capsule does not touch the walls beside the player. Second, the bottom starts above the feet so the floor itself is not counted. Set Obstacle Mask in the Inspector to everything except the Player layer, otherwise the player’s own collider blocks standing forever.
A cheaper alternative is a single upward raycast or sphere cast from the head. It is fine for tunnels but can miss a ceiling edge at the capsule’s side, so the capsule check is safer. The raycast tutorial covers casts and layer masks in depth.
Smooth Camera Height
The script lerps the camera holder’s local Y, which gives a nice dip when crouching. A few notes:
- Move a holder object, not the Cinemachine camera or the main camera directly, if the camera has other scripts that set its position.
- If the camera jitters, make sure only one script writes to its position, and that movement and camera updates happen in a consistent order.
- Keep the eye height below the top of the capsule. At a crouch height of 1, an eye height of 0.8 sits 0.2 below the top, so the camera never pokes through a low ceiling.
Crouch Speed and Sprint
Expose IsCrouching so your movement script can read it:
// in your movement script
[SerializeField] PlayerCrouch crouch;
[SerializeField] float walkSpeed = 5f;
[SerializeField] float crouchSpeed = 2.5f;
float CurrentSpeed => crouch.IsCrouching ? crouchSpeed : walkSpeed;
Block sprinting while crouched, or let sprint cancel the crouch if there is room. If you have a stamina system, crouching should not drain it. See the stamina and sprint guide for how to structure that state. For stealth, a lower crouch speed can also shrink an enemy’s detection range.
Also think about slopes: a shrunken capsule has a lower step offset in practice, and crouch-walking along a ramp can cause the grounded flag to flicker. The slope movement guide explains the fixes.
Rigidbody and Other Capsules
With a Rigidbody and a CapsuleCollider, the same logic applies. Change collider.height and collider.center, and run the same CheckCapsule test before standing. Two differences:
- When you shrink a Rigidbody collider, the body can fall slightly for a physics step. Keep the bottom of the capsule fixed, as above, to avoid that.
- When you grow it, an overlapping collider pushes the body out with real physics forces and can launch the player. The ceiling check is not optional.
Never scale the player’s Transform to crouch. Scaling a CharacterController’s transform does not behave predictably, and scaling a Rigidbody scales its colliders and children in ways that cause bugs in lighting, audio and child objects. Change collider values instead.
🔧 Speed up your character setup
Character controller kits and animation packs can save days of work. See our recommended Asset Store tools.
Troubleshooting
Player floats above the ground when crouching
Only the Height was changed. Set Center to half the new height (when the pivot is at the feet) every time you resize.
Player gets stuck crouched forever
The ceiling check is hitting the player’s own collider, or the floor. Exclude the Player layer from the obstacle mask and start the capsule above the feet.
Player pops through the ceiling when standing
There is no ceiling check, or it is a thin raycast that misses the edge. Use Physics.CheckCapsule with the full standing size.
Camera snaps instead of moving smoothly
You set the camera position directly instead of lerping. Lerp toward the target height each frame with Time.deltaTime.
Crouch key does nothing
Check that the script is on the player, that cameraHolder is assigned, and that you have the Input System package active (Project Settings > Player > Active Input Handling). If it is set to the old Input Manager only, Keyboard.current will not work.
Player falls through the floor after crouching
The controller center is below the pivot. Check the numbers, and see the player falls through floor guide for other causes.
FAQ
Should crouch be a toggle or a hold?
Offer both in the settings menu. Hold is more responsive for action games; toggle is more comfortable for long stealth sections.
How do I add a crouch animation?
Set an Animator bool from IsCrouching and use it as the condition for a crouch blend tree or state. Keep the capsule logic in code, not in the animation.
Can the player crouch in the air?
You can allow it, but then you need to move the capsule bottom up so the feet do not stick out below. Many games block crouching while airborne.
What crouch height should I use?
About half the standing height. 1 meter against a 2 meter capsule is common. Check that the player can still fit through your smallest gaps.
Does this work for NPCs with NavMeshAgent?
No. NavMeshAgent has its own height setting that affects pathing. Change the agent’s height only if you want it to fit under obstacles, and bake the NavMesh with a matching agent type.
Reader questions
Asked by readers of this article and answered by AI. Behavior can differ between Unity versions, so test in yours.




Leave a Reply