CharacterController glitches if it reaches a ledge while jumping into a wall.

I’m trying to make a script for a Character Controller that allows for movement changes midair and clamps the magnitude of movement when keys of two axes (so a character doesn’t move faster in the diagonals). I have modified the script provided in Unity - Scripting API: CharacterController.Move and done these things.

However, if I hold forward to keep moving into a wall with a ledge and jump (while still holding forward), the character just glitches when it reaches the ledge. It sort of phases very slightly into the wall and hangs for a moment before dropping back down. The original script in the link above does not behave this way with ledges.

Example gif

The first two jumps are not against the wall.

How would I fix this?

    void Update()
    {
        leftRight = Input.GetAxis("Horizontal");
        forwardBackward = Input.GetAxis("Vertical");
        rotX = Input.GetAxis("Mouse X");
        //rotY = Input.GetAxis("Mouse Y");
        transform.Rotate(Vector3.up, rotX * sensitivity * Time.deltaTime);

        if (characterController.isGrounded)
        {
            moveVertical = new Vector3(0, 0, 0);

            if (Input.GetButton("Jump"))
            {
                moveVertical.y = jumpSpeed;
            }
            Debug.Log("Currently grounded.");
        }
        else
        {
            Debug.Log("Currently airborne.");
        }

        if (Input.GetButton("Horizontal") && Input.GetButton("Vertical"))
        {
            leftRight = Mathf.Clamp(leftRight, -0.707f, 0.707f);
            forwardBackward = Mathf.Clamp(forwardBackward, -0.707f, 0.707f);
        }

        if (forwardBackward != 0)
        {
            characterController.Move(transform.forward * speed * Time.deltaTime * forwardBackward);
        }
        if (leftRight != 0)
        {
            characterController.Move(transform.right * speed * Time.deltaTime * leftRight);
        }

        moveVertical.y -= gravity * Time.deltaTime;
        characterController.Move(moveVertical * Time.deltaTime);
        //Debug.Log(moveVertical.y);
    }

Yo! I found out how to fix it! I’ve been having this problem ever since I made my player object. I didn’t like changing the skin thickness cause it didn’t look or feel good when playing. The problem is that your player is getting confused if it should climb or what when you jump because your step offset is in effect. What I did was set the step offset to 0.5 (or literally whatever you want), and set it to 0 when you’re in mid-air. Then, once you land back on the ground, set it back to your original value. Here’s an example:


    if(isGrounded)
    {
    controller.stepOffset = 0.5f;
    }
    else
    {
    controller.stepOffset = 0f;
    }

Try setting your Step Offset value to something smaller like 0.1.
When you jump up near an edge your Step Offset comes into account.

Try playing around with the “Skin Width”-Attribute on your Character Controller.
_
The Manual (https://docs.unity3d.com/Manual/class-CharacterController.html) says:
“Two colliders can penetrate each other as deep as their Skin Width. Larger Skin Widths reduce jitter. Low Skin Width can cause the character to get stuck. A good setting is to make this value 10% of the Radius.”
_
From my personal experience, it seems that those 10% are not enough sometimes.

Same problem here with similar code.

Try setting your slope limit to 90 when your character jumps.

For example:

if (jump)
{
     characterController.slopeLimit = 90;
}
else
{
     characterController.slopeLimit = 45;
}