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 https://docs.unity3d.com/ScriptReference/CharacterController.Move.html 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.
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);
}
Is there any particular line or set of lines in your code which, when outcommented, get rid of the glitch you describe on ledges? If so, which ones are they?
Answer by nickbireli · Aug 22, 2021 at 09:38 PM
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;
}
Answer by NathoSteveo · Dec 05, 2020 at 11:38 PM
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.
Yea you're right it's the step offset that creates the glitch, but then when we reduce it we can't go up stairs :/
Answer by SpyZzey · Jul 11, 2020 at 12:47 PM
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.
sometimes? more like never. this will not work unless you want to make the character controller the size of a tank and completely annihilate any systems you may have that are dependent on the size of it.those 10% are not enough sometimes
Answer by HairlessGorilla · Apr 17, 2021 at 09:10 AM
Try setting your slope limit to 90 when your character jumps.
For example:
if (jump)
{
characterController.slopeLimit = 90;
}
else
{
characterController.slopeLimit = 45;
}
i implemented a similar solution, except that it was with step offset(since step offset is what causes the problem), i set it to 0 while jumping and back to the intended value when on ground