Character Controller Crouching Jitter Issue [SOLVED]

hello guys, i’ve run a bit into a problem, basically when my character goes to crouching, everything goes smoothly, but when I un-crouch, I get this jittery motion going back up, which is kinda annoying because, visually it’s really bad…

although i’ve searched online for potential fixes, and there are answers regarding about character controller center and “camera positions”, obviously im not the guy that just straight copy pastes everything, so I find it hard what is like the efficient way to do it… here’s the code

    void Crouch()
    {
        if (canCrouch == true)
        {
            if (Input.GetKey(crouchKey))
            {
                isCrouching = true;
                walkingSpeed = crouchingSpeed;
                controller.height = Mathf.MoveTowards(controller.height, 1.0f, Time.deltaTime / crouchTimeDown);
                Debug.Log("Crouching");
            }
            else if (!Input.GetKey(crouchKey) && touchedCeiling)
            {
                isCrouching = true;
                walkingSpeed = crouchingSpeed;
            }
            else
            {
                isCrouching = false;
                controller.height = Mathf.MoveTowards(controller.height, 2.0f, Time.deltaTime / crouchTimeUp);
            }
        }
    }

UPDATE:
So, upon trying things out, the jitter problem was lessen visually, or that’s what I think happened by trying to meddle with the center Y axis of the character controller.

    void Crouch()
    {
        if (canCrouch == true)
        {
            if (Input.GetKey(crouchKey) && isGrounded)
            {
                isCrouching = true;
                walkingSpeed = crouchingSpeed;
                controller.height = Mathf.MoveTowards(controller.height, standingHeight / 2, (5.0f * Time.deltaTime));
                controller.center = Vector3.MoveTowards(controller.center, crouchingCenter, (5.0f * Time.deltaTime));
                Debug.Log("Crouching");
            }
            else if (!Input.GetKey(crouchKey) && touchedCeiling)
            {
                isCrouching = true;
                walkingSpeed = crouchingSpeed;
            }
            else
            {
                isCrouching = false;
                controller.height = Mathf.MoveTowards(controller.height, standingHeight, (5.0f * Time.deltaTime));
                controller.center = Vector3.MoveTowards(controller.center, standingCenter, (5.0f * Time.deltaTime));
            }
        }
    }

@klay03, You are dividing Time.deltaTime with crouchTimeUp and crouchTimeDown. Try multiplying it instead. I’m not sure but it’s worth giving it a try.

Hey, thanks for the post i was posting the same issue as you and i jumped to this post i am still waiting for the answer.

FINAL UPDATE:

Alas!, I have fixed it, what was needed to be done was not I was really expecting.
@AnnabelleParisian

Upon searching the internet for potential fixes, I came across this Youtube video by Hero 3D, which talks about how to do smooth crouching: - YouTube


So to save you from reading a bunch of nerdy stuff, I’m not gonna explain everything, watch the video if you want to fully understand the code itself. Upon trying out his code the first try, it didn’t work, so what I did was tweak his script a little so that it would work with my script. First, here are the needed variables:


public Transform playerCamera;

private float crouchingSpeed = 2.5f;
private float timeToCrouch = 5.0f;
private float standingHeight = 2.0f;
private float crouchHeight = 1.0f;

With those variables set up, let’s view the Crouch() function.


    void Crouch()
    {
        float crouchedHeight = isCrouching ? crouchHeight : standingHeight;

        if (controller.height != crouchedHeight)
        {
            AdjustController(crouchedHeight);

            Vector3 camPosition = playerCamera.transform.localPosition;
            camPosition.y = controller.height;

            playerCamera.transform.localPosition = camPosition;
        }

        if (isCrouching)
        {
            walkingSpeed = crouchingSpeed;
        }
    }

So what this Crouch() function does is it handles if we are crouching or not, with the first line:
float desiredHeight = isCrouching ? crouchHeight : standingHeight:
we are going to put a condition that if the player is indeed crouching, then we are getting the crouhHeight variable, then on the "if " statement below, if the height of the controller does not equal to our crouchedHeight, we then adjust the controller to our said crouchedHeight.


    private void AdjustController(float height)
    {
        float center = height / 2;

        controller.height = Mathf.LerpUnclamped(controller.height, height, timeToCrouch * Time.deltaTime);
        controller.center = Vector3.LerpUnclamped(controller.center, new Vector3(0, center, 0), timeToCrouch * Time.deltaTime);
    }

So here is the actual function of which the action of crouching takes place. You can see that it is in LerpUnClamped, because we all know that Lerp only clamps its “t” between 0 and 1, but since in my case, the crouch was too slow, so I changed it into LerpUnClamped so that the “t” for this Lerp would exceed between 0 and 1.


I hope that wasn’t confusing at all xd, well I was really confused myself too but I found a way to fix it myself.


P.S
You should set the center Y axis of the character controller to 1, with a height of 2.