8

I have following simple scene: scaled box at (0, 0, 0) for floor and 1 size box at (0, 2, 0). I try to do simple gravity and use following code:

private float gravity = 1.0f;
private Vector3 moveVector;

void Update()
{
    if (characterController.IsGrounded)
    {
        Debug.Log("is grounded");
        verticalVelocity = 0;
    }
    else
    {
        Debug.Log("not grounded");
        verticalVelocity -= gravity;
    }

    Debug.Log("vertical velocity:" + verticalVelocity);

    moveVector.x = 0;
    moveVector.y = verticalVelocity;
    moveVector.z = 0;

    characterController.Move(moveVector * Time.deltaTime);
}

I see in log that event when object is on the floor I have grounded/not grounded messages. Visually object is on the floor and not oscillating. Why I don't have constant "is grounded" when object is on the floor? Maybe it is how CharacterController works but I can't find anything about it in documentation.

2
  • Can we see the code where characterController.IsGrounded is set? Commented Sep 28, 2016 at 10:58
  • @ḞḹáḿíṅḡⱫỏḿƀíé this property is controlled by Unity engine. I don't set it. Commented Sep 28, 2016 at 17:01

5 Answers 5

7

You have to set Character Controller Min Move Distance to '0' (zero) or else you will always get this behavior, it is trying to limit how many times it checks if it's grounded, but at the end of the day gamers don't care, they will mash the jump button and call it buggy

3
  • This comment is not relevant. Min Move Distance does not affect this bug. Commented Jul 26, 2023 at 12:09
  • This might be true. I have a github project which seems to me to be very stable and performant in which I followed a different approach: github.com/koyima/fps-tutorial
    – koyima
    Commented Jul 27, 2023 at 13:33
  • what exactly did you do to solve it? Commented Jul 28, 2023 at 15:15
4

Had the same issue

Just had some gravity when the character controller is grounded

Replace verticalVelocity = 0;

By verticalVelocity = -gravity * Time.deltaTime;

1
  • Unfortunately problem still here: isGrounded produce true and false. Do you have code example with correct logic? Commented Oct 21, 2016 at 20:41
3

Setting "Min Move Distance" to 0 in the Character Controller Inspector solved a very similar problem for me.

1

Strubble is right. You have to add some small gravity even if character is grounded.

if (characterController.IsGrounded)
{
    // Press the character down to the floor to avoid jitter "true-false"
    // of the isGrounded property.
    // To do it, add some small gravity (or velocity in your terms).
    verticalVelocity = -gravity * 0.1f;
}
0

just use a Raycast with Vector3.down because I've tired of fixing it and it's really unreliable to use even while prototyping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.