Player goes through the ground when crouching under small areas

made a character that crouch, and altered it’s collider in code so it can go under small areas, it works fine but if uncrouch while being under a small area the character clips through the ground for some reaso, is there a fix to that?
thank you.
the crouching script:
void OnCrouch(InputValue value)

{

    crouchInput = value.isPressed;

}

void Crouching()

{

if(crouchInput)

    {

    myAnimator.SetBool("isCrouching", true);

    myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x*0.6f, myRigidbody.velocity.y);

    bodyCollider.size = new Vector2 (bodyCollider.size.x, 0.9145353f);

    bodyCollider.offset = new Vector2 (bodyCollider.offset.x,-0.0611621f);

    }

else

    {

    myAnimator.SetBool("isCrouching", false);

    bodyCollider.size = new Vector2 (bodyCollider.size.x, 1.158416f);

    bodyCollider.offset = new Vector2 (bodyCollider.offset.x,0.06077838f);

    }

}

these are the colliders dimensions

i had an idea where i create another collider above the player and say that if it’s colliding with something it should keep these values until it’s no longer colliding, but i feel like there’s a simpler way

Don’t allow them to uncrouch.

I had a similar issue once. I fixed it by having a trigger collider at the character’s head that stays where it is when crouching. If I uncrouched and this trigger is still inside something, I’d unset the ‘crouching’ flag, but left the character in the crouch position. So, I would only uncrouch the character if the crouch key is not being pressed AND the trigger is not inside something (or something inside it).

This was probably not the best fix but it worked and I left it in. Until I eventually removed the crouch ability altogether


Edit

Yeah, this is what I did

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms