Search Unity

Seems like CharachterController.IsGrounded doesn't work properly

Discussion in 'Physics' started by DroidifyDevs, Apr 25, 2018.

  1. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Hello!

    I'm trying to make an object with a character controller component have gravity. It thought it would be as simple as this:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class FPSControllerNOJUMP : MonoBehaviour
    5. {
    6.     public float moveSpeed;
    7.     public bool IsGrounded;
    8.     public float currentJumpSpeed;
    9.     public CharacterController CC;
    10.  
    11.     void FixedUpdate()
    12.     {
    13.         if (CC.isGrounded)
    14.             currentJumpSpeed = 0;
    15.         if (!CC.isGrounded)
    16.             currentJumpSpeed -= 0.1f;
    17.         IsGrounded = CC.isGrounded;
    18.         CC.Move(new Vector3(Input.GetAxis("Horizontal") * moveSpeed, currentJumpSpeed, Input.GetAxis("Vertical") * moveSpeed));
    19.     }
    20. }
    However, when falls to the ground, it switches between isGrounded and !isGrounded, seemingly randomly. What I'm trying to say is that the object is no longer moving, but isGrounded still changes as it wishes, while the object is still on the ground.

    What could cause this in my very simple code?

    Thank you!