CharacterController is he grounded ?

Hello everybody,

We are coding a 3D side scroller game (like Trine for example).

The main character is definded by a CharacterController with a script to manage movements.

For each frame, I check if my player is grounded or not. With this information, I’m able to allow or avoid specific movement.

For instance, I can trigger a roll when I’m grounded but I can’t when I’m jumping.

Here is my IsGrounded function :

bool IsGrounded (){

bool isGroundedBool = false;

CharacterController controller = GetComponent<CharacterController>();

if ((controller.collisionFlags & CollisionFlags.Below) != 0)

isGroundedBool = true;

return isGroundedBool;

}

My problem is I have completely random output from this function.

I tried to use CharacterController.isGrounded() with the same results.

My character is moving on a Mesh Collider, a Mesh Filter and a Mesh Rendered.

Can you explain me why I have different outputs even when my CharacterController staying on the ground (no movement).

Thanks a lot ! (sorry for my awful english …)

I’ve had problems like this before. Make sure the player controller is always moving down a little bit, even down by 0.01f each frame. That usually fixes it for me, and nothing actually changes.

collisionFlags only returns a result for a specific direction if the last Move call actually moved into a surface in that direction.

So if you’re standing still, and the Move call isn’t pushing down at all, it won’t hit the ground, and won’t return true for Below. This could also occur while walking forward down a slope, where you’ll get:

true
false
false
true
false
false
true

Because you’re really pop off the surface, than gravity pulls you back down, than you pop off the surface. It’s small, but it’s there in regards to actual collision information.

Use a raycast instead.

Nice, thanks you very much for your replies ! =)