How to keep CharacterController grounded without constantly applying gravity?

Ok, this is seemingly an extremely simple question. Whenever I apply Y movement (gravity) to a charactercontroller, the expected behavior happens and the object falls down until it hits the ground. The issue is when I stop applying gravity, the CharacterController constantly fluctuates between isGrounded = false and isGroudned = true. I’ve stripped down literally everything except for a few lines of code to test with:

 void Update
{
    		Debug.Log( "is grounded: " + controller.isGrounded );
    		moveDirection.y -= gravity * Time.deltaTime;
    		controller.Move( moveDirection * Time.deltaTime );
}

In the code above, once the charactercontroller falls to the ground, isGrounded keeps returning true as expected. When I try this code…

		Debug.Log( "is grounded: " + controller.isGrounded );
		if( !controller.isGrounded ) 
		{
			moveDirection.y -= gravity * Time.deltaTime;
			if( moveDirection.y < -maxFallSpeed ) moveDirection.y = -maxFallSpeed;
		}
		else moveDirection.y = 0;
		controller.Move( moveDirection * Time.deltaTime );

…the controller fluctuates isGrounded values every few frames.

How do I keep the controller grounded without having to constantly apply gravity? Once it’s grounded, shouldn’t it stay grounded until I apply movement in the opposite direction? Thanks.

I suspect that isGrounded is set by collisions in the bottom side of the CharacterController. If you wait the CC (CharacterController) to become grounded, then remove the gravity and move it horizontally, isGrounded becomes false. In your code, gravity is applied as a downwards velocity whenever the isGrounded is false; when it becomes true, you cancel the vertical velocity, what makes isGrounded turn to false - gravity is applied again, isGrounded becomes true, gravity is suspended, and so on, creating a loop that flips isGrounded true/false continuously.

Why do you want to suspend gravity? Usually, the gravity is continuously applied in order to keep the CC grounded, and there’s nothing wrong with this.

Hi!
I solved this problem when added strange code “-= 0;”

 void Update()
    {
        isGrounded = controller.isGrounded;

        if (isGrounded)
        {
            verticalVelosity -= 0;
        }
        else
        {
            verticalVelosity -= 1;
        }

        moveVector = new Vector3(0, verticalVelosity, 0);
        controller.Move(moveVector);
    }

What if it collides with the ground when you apply downward force? Won’t it bounce up in the air?