unity – .isGrounded is not working reliably

New to Unity and C#, though, I got the code from the official doc page for Character Controller and thought that should work by default.

When pressing the Jump button here it doesn’t always work. When checking if the player is grounded the variable groundedPlayer not always provides true when idle/not moving. Instead the value is constantly changing between true and false.

Below is my current code which was modified a little:

public class PlayerController : MonoBehaviour
{
  private CharacterController controller;
  private Vector3 playerVelocity;
  private bool groundedPlayer;
  private float playerSpeed = 2.0f;
  private float jumpHeight = 1.0f;
  private float gravityValue = -9.81f;

  private void Start()
  {
    controller = gameObject.AddComponent<CharacterController>();
  }

  void Update()
  {
    groundedPlayer = controller.isGrounded;
    if (groundedPlayer && playerVelocity.y < 0)
    {
      playerVelocity.y = 0f;
    }

    Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    controller.Move(move * Time.deltaTime * playerSpeed);

    if (move != Vector3.zero)
    {
      gameObject.transform.forward = move;
    }

    print(groundedPlayer); //this will constantly return both false and true while idle

    // jumping mechanic
    if (Input.GetButtonDown("Jump") && groundedPlayer)
    {
      playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
    }

    playerVelocity.y += gravityValue * Time.deltaTime;
    controller.Move(playerVelocity * Time.deltaTime);
  }

}

It’s just that I feel dumb since basic jumping is this difficult to implement for me. I feel like I’m missing something very obvious. I found 1 working solution for me which is implementing coyote time, but it looks like a workaround and might provide more glitches in future. I also could try working on creating my own character controller, though that’s a whole different field.

Anyone had this issue before? How did you fix it?