Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

CharacterController.IsGrounded

Discussion in 'Physics' started by Goodeddie, Aug 12, 2016.

  1. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    Hello.

    I know there are plenty of threads that talk about the IsGrounded being "unreliable" but I think that someone must've found a solution to using it.

    My code calls a Move method first so that the character can jump. Even though when you are grounded and the y velocity is 0, the Move method sets the IsGrounded to false for some reason.
    Afterwards, I call the SimpleMove with a vector3 of zero to simulate gravity. This is the part I don't get. At some point, the IsGrounded gets set to true which is correct. But sometimes (even though I call the Move method with ZERO velocity) the SimpleMove method sets the IsGrounded to false. Why is that?
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    If I recall correctly, IsGrounded is set true if the bottom of the controller intersects an object during a Move. Pretty sure if CollisionFlags.Below is set to 1, then so too is IsGrounded. So if your object doesn't intersect anything at it's feet, it's not grounded.

    What I do is just fire a SphereCast at the player's feet and use that to check if they're grounded. Gives me much more control over everything.
     
  3. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    Does SphereCast work 100% of the time?
     
  4. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    This is what I have in my Update method.

    Debug.Log(characterController.isGrounded.ToString());
    characterController.SimpleMove(Vector3.forward * 0); // Use world gravity

    The CharacterController is above a flat plane and for some reason, even though it stops on the plane, it still sometimes logs False
     
  5. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    I don't know what the hell I changed but the Move method is not setting the isGrounded to false anymore...
     
  6. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    Oh I think it has something to do with me resetting the jumpDirection.y to 0...I'll try to figure it out. Thanks
     
  7. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    So I have code where if the character is grounded, then reset the y velocity because otherwise then the y velocity will keep decreasing and when you fall you'll fall really fast. Apparently, if you set the y velocity too small (0 for example), I'm guessing the isGrounded flag is set to false. Then it'll see you're colliding and set it back to true but then my code resets the y velocity again and the cycle repeats.
     
  8. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    So to kind of fix this, instead of setting it to 0, I've set it to a number low enough so that it doesn't jump into that cycle. In my case, -0.1 works and the fall speed is barely noticable.
     
  9. hawaiian_lasagne

    hawaiian_lasagne

    Joined:
    May 15, 2013
    Posts:
    120
    Hi, I was having the same problem where setting a velocity.y of 0 would cause isGrounded to become false. By allowing a minimum move distance of 0 fixed it for me alternating between isGrounded true/false. Even the Unity docs recommend setting this value to 0, but then they default it to 0.001!
     
    QueryWhere likes this.