- Home /
CharacterController isGrounded toggles value
My character controller isGrounded toggles its value even if the movement vector magnitude is zero. And when I walk it keeps toggling its value. So, if I want to jump, sometimes it doesn't respond because it says it's not grounded. But when you're playing it looks completely grounded. What do you think is happening?
public float walkSpeed;
public float rotationSpeed;
public float gravityAcceleration;
public float verticalSpeed;
public float jumpVerticalSpeed;
private CharacterController controller;
public bool grounded;
void Start () {
controller = GetComponent(typeof(CharacterController)) as CharacterController;
}
void Update () {
Vector3 movement = Vector3.zero;
movement += transform.forward * Input.GetAxis("Vertical") * walkSpeed * Time.deltaTime;
grounded = controller.isGrounded;
if(!controller.isGrounded){
verticalSpeed += gravityAcceleration * Time.deltaTime;
movement += transform.up * verticalSpeed * Time.deltaTime;
}else{
if(Input.GetKeyDown(KeyCode.LeftControl)){
Debug.Log("jump");
verticalSpeed = jumpVerticalSpeed;
}else
verticalSpeed = 0;
}
transform.Rotate(0,Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime,0);
controller.Move(movement);
}
I have a public bool variable in order to see controller.isGrounded value in the inspector.
Answer by LukeWaffel · Sep 28, 2015 at 10:17 PM
Actually @gnp89 and @armirblum , there is a very good reason for this,
When the charactercontroller touches a service, it shows it's grounded. When your script notices this, it sets the vertical velocity to 0, which means, there is no change in velocity. But this also means, the player get's pushed up a really tiny bit because of the ground and the collision and stuff. So when it moves up this tiny tiny bit, it get's off the ground, which makes the controller think it's not grounded anymore. When your script picks up this change, it will force it down because you implemented fake gravity. And then it happens again and again.
To fix this issue, you simply set the downwards velocity to -0.1f, or another small value, instead of 0. This means, the player will be forced down always, just by a really tiny amount. this way, the charactercontroller will always think it's grounded, and you will always jump when needed.
Thank you for your answer. I just wanted to share how little I needed to do to fix this since it feels so little yet made such a large difference. This is what my code was before:
CharControl.$$anonymous$$ove(new Vector3(0, VerticalVelocity * Time.deltaTime, 0));
VerticalVelocity -= Gravity;
And this is what my code was after:
VerticalVelocity -= Gravity;
CharControl.$$anonymous$$ove(new Vector3(0, VerticalVelocity * Time.deltaTime, 0));
so to fix my problem (in visual studio) all I had to do was press ALT + up arrow to fix my problem.
Answer by lucidcloud · Oct 23, 2020 at 04:23 AM
@gnp89 I rarely use the isGrounded method of the character controller, since even with all the changes you can make collisions still make it buggy. Instead it the Update function, I get the position of a gameObject using gameObject.transform.position, and only jump if that position is less than any float between 0.2 to 0.5. This depends on what value you are receiving when you use transform.position. If the gameObject has already jumped, then the position will be greater than the the float, so you can use it as a condition along with Jump.triggered to make a character jump. If you want to prevent jumping when a character falls off a platform, simply do -0.5 < pos.y < 0.5.
I don't think that's a very good solution. That only works if in your game the character never goes up or down unless it's jumping. So no stairs, no elevators, no slopes.
Your answer
Follow this Question
Related Questions
Adding gravity to character and grounded checks are not working? 0 Answers
When my player climb something, after a certain point it start to float 0 Answers
Problem with CharacterController.isgrounded 1 Answer
Sphere + rigidbody + character controller 1 Answer
How reliable are isGrounded checks? 1 Answer