DEV Community

Jin
Jin

Posted on

Standing Up Collision Check (Crouch Stand)

Before standing up, Check if there is enough space above the player.
Use Physics.CheckCapsule with the standing height, not the crouching height.
The capsule starts at the player’s bottom position and extends upward to the standing height.
Player and ground layers are excluded from the check.
If the capsule overlaps with any collider, standing up is blocked.

bool CanStandUp()
{
    float radius = characterController.radius;

    // bottom of player
    Vector3 bottom = transform.position + characterController.center - Vector3.up * characterController.height * 0.5f;

    // top of player when standing
    Vector3 top = bottom + Vector3.up * standHeight;

    // for debug
    Debug.DrawLine(bottom, top, Color.red);  

    int layerMask = ~LayerMask.GetMask("Player", "Ground");

    // Check Capsule Collision
    return !Physics.CheckCapsule(
        bottom,
        top,
        radius,
        layerMask,
        QueryTriggerInteraction.Ignore
    );
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)