Character Collision flags rapidly flashes

I’m having trouble with this piece of code. Specifically I’m trying to code this collision system below the character controller that can tell me if I’m grounded or not. The only problem is that the isGrounded bool rapidly switches.

The Character Controller code also includes a jump and gravity system as well as a progressive movement system (Slowly builds up speed) .

Here’s the essential code. I hope you guys can find the solution to the problem.

void GroundedCheck()
    {
        if (characterController.collisionFlags == CollisionFlags.Below || (characterController.collisionFlags & CollisionFlags.Below) != 0)
        {

            isGrounded = true;
        }
        else
        {
            isGrounded = false;
        }
    }

    void Jumping()
    {
        if (isGrounded == true)
        {
            JumpSpeed = 0;

            if (Input.GetButtonDown("Jump"))
            {
                JumpTimer = 0f;
                JumpSpeed = JumpIntensity;
            }
        }
        if (isGrounded == false)
        {
            if (JumpTimer == JumpInterval){                                     //Check if JumpTimer is up
                JumpSpeed -= GravitySpeed;                                      //Lower Jump Speed
                if (JumpSpeed <= -JumpGravity)                                  //Check if jump speed is faster than gravity
                {
                    JumpSpeed = -JumpGravity;                                   //Set Speed to max gravity
                }
            }
            else if (JumpTimer < JumpInterval && Input.GetButton("Jump"))       //Check if JumpTimer is under Max and check if Jump button is held down    
            {
                JumpTimer += Time.deltaTime;                                    //Add time to Timer
                JumpSpeed = JumpIntensity;                                      //Keep Jump going
            }
            else                                                                //if the button is not held or the Jump timer is over interval
            {
                JumpTimer = JumpInterval;                                       //Set JumpTimer at end
            }
        }
    }

Also, Quick Question: Is it practical to use Char collision flags or is there another accurate way to check collisions? Thanks in advance!

I have never used those collision flags or the characterController component, but that doesn’t mean they’re not good. There are many ways in wich you can check for the ground. Usually they involve trhowing a small ray downwards and see if it hits something, it won’t give those false positives you’re having. Regarding your current script, an option may be to just check if the collision bellow returns true for a couple of frames, say 12 in a row (half a second) before deciding that indeed he’s not grounded.

I’m not sure if the component you’re using is rather old or what, but I haven’t seen it before. You may want to just get one example from the standard assets, if you’re aiming for 3D the “Third person controller” may be a good start. It works right out of the box, and you can study it’s code and modify it to learn about it. Doing your first controller is a bit difficult, you could use some tutorial like the ones on youtube, or better one the Unity ones. The advantage is that they already give you the models and the animations and they guid you through the process.

I forgot to say that when the player’s on like a slope I want to make sure it stays on the floor. Would i still use rays?

yeah, a raycast to the ground will do for slopes, just make sure you cast it from the center and that it has some length to not get a positive when the character just bounce slightly. You can use Debug.DrawRay to see it in the editor screen so that you can know if it’s well placed. Also, you may want to not throw one ray every frame, this way it consume less resources. Remember that there are many frames in a second, and you don’t need to check that match.
You can also just stick with what you have and just recheck a couple of frames before concluding it’s not in the ground. Also you can try to see it frame by frame and try to figure why it’s bouncing.