Issue with isGrounded / falling in animation state controller script

Hi,
I have a script for the animation state controller in my 3D game. The problem is that sometimes the player is on the ground, but the falling animation is playing for no reason, and the player won’t stop falling until I jump or fall and then hit the ground very hard. Here’s the script (I know it’s a really bad script, i’m planning of recoding it) :


public class AnimationStateController : MonoBehaviour
{
    Animator animator;
    private Rigidbody rb;
    private CharacterController controller;
    public GameObject seaVisual;
    public PlayerMotor playerMotor;

    // private CharacterController characterController;
    // [SerializeField] private float groundRaycastDistance = 2.0f;

    void Start()
    {
        animator = GetComponent<Animator>();
        controller = GetComponent<CharacterController>();
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        MyAnimationState();        
    }

    private void MyAnimationState()
    {
        // bool declaration part

        // animator bool
        bool isWalking = animator.GetBool("isWalking");
        bool isWalkingBackward = animator.GetBool("isWalkingBackward");
        bool isWalkingLeft = animator.GetBool("isWalkingLeft");
        bool isWalkingRight = animator.GetBool("isWalkingRight");
        bool isJumping = animator.GetBool("isJumping");
        bool isFalling = animator.GetBool("isFalling");
        bool isGroundedAnimation = animator.GetBool("isGrounded");
        bool isLanding = animator.GetBool("isLanding");
        bool isSwimming = animator.GetBool("isSwimming");
        bool isPistolAimingIdle = animator.GetBool("isPistolAiming");


        // bool isCrouch = animator.GetBool("isCrouch");
        // bool isWalkingCrouch = animator.GetBool("isWalkingCrouch");
        
        // key bool declaration
        bool runPressed = Input.GetKey(KeyCode.LeftShift);
        bool forwardPressedCtrl = Input.GetKey(KeyCode.LeftControl);
        bool forwardPressedW = Input.GetKey(KeyCode.W);
        bool forwardPressedS = Input.GetKey(KeyCode.S);
        bool forwardPressedA = Input.GetKey(KeyCode.A);
        bool forwardPressedD = Input.GetKey(KeyCode.D);
        bool forwardPressedSpace = Input.GetKey(KeyCode.Space);
        bool aimButton = Input.GetKey(KeyCode.Mouse1);

        // Sea animation part
        Vector3 characterPosition = transform.position;
        Vector3 waterPosition = seaVisual.transform.position;
        float heightAboveWater = characterPosition.y - waterPosition.y;
        float waterThreshold = -1.1f;
        bool isInWater;
        

        if (heightAboveWater < waterThreshold) {
            isInWater = true;
        } else {
            isInWater = false;
        }

        if (isInWater) {
            animator.SetBool("isSwimming", true);
        }
        if(!isInWater) {
            animator.SetBool("isSwimming", false);
        }

        // W
        // if (isInWater && forwardPressedW) {
            
        // }
        // if (isInWater && !forwardPressedW) {

        // }

        // S

        // A

        // D

        // walking part
        // W
        if (!isWalking && forwardPressedW){
            animator.SetBool("isWalking", true);
        }
        if (isWalking && !forwardPressedW){
            animator.SetBool("isWalking", false);
        }

        // S
        if (!isWalkingBackward && forwardPressedS) {
            animator.SetBool("isWalkingBackward", true);
        }
        if (isWalkingBackward && !forwardPressedS) {
            animator.SetBool("isWalkingBackward", false);
        }

        // A
        if (!isWalkingLeft && forwardPressedA) {
            animator.SetBool("isWalkingLeft", true);
        }
        if (isWalkingLeft && !forwardPressedA) {
            animator.SetBool("isWalkingLeft", false);
        }

        // D
        if (!isWalkingRight && forwardPressedD) {
            animator.SetBool("isWalkingRight", true);
        }
        if (isWalkingRight && !forwardPressedD) {
            animator.SetBool("isWalkingRight", false);
        }

        // running part 
        if (forwardPressedW && runPressed){
            animator.SetBool("isRunning", true);
        }
        if (!forwardPressedW || !runPressed){
            animator.SetBool("isRunning", false);
        }

        // S run
        if (forwardPressedS && runPressed) {
            animator.SetBool("isRunningBackward", true);
        }
        if (!forwardPressedS || !runPressed) {
            animator.SetBool("isRunningBackward", false);
        }

        // A run
        if (forwardPressedA && runPressed) {
            animator.SetBool("isRunningLeft", true);
        }
        if (!forwardPressedA || !runPressed) {
            animator.SetBool("isRunningLeft", false);
        }

        // D run
        if (forwardPressedD && runPressed) {
            animator.SetBool("isRunningRight", true);
        }
        if (!forwardPressedD || !runPressed) {
            animator.SetBool("isRunningRight", false);
        }


        // jumping && falling part
        bool checkGrounded = CheckGrounded();

        if (checkGrounded) {
            animator.SetBool("isGrounded", true);
        }
        if (!checkGrounded) {
            animator.SetBool("isGrounded", false);

            // if (playerMotor.GetPlayerMotorVelocity() < -1f && playerMotor.GetPlayerMotorVelocity() > -3f) { 
            //     animator.SetBool("isGrounded", true);
            // } else {
            //     StartCoroutine(CheckGroundedSeconds());
            //     if (playerMotor.GetPlayerMotorVelocity() < -3f || playerMotor.GetPlayerMotorVelocity() > 0) {
                    // animator.SetBool("isGrounded", false);
            //     }
            // }
        }
        

        if (forwardPressedSpace && !isJumping) {
            animator.SetBool("isJumping", true);
        }
        if (!forwardPressedSpace && isJumping) {
            animator.SetBool("isJumping", false);
        }


        if (!isGroundedAnimation) {
            animator.SetBool("isFalling", true);
        }
        if (isGroundedAnimation) {
            animator.SetBool("isFalling", false);
        }

        if (isLanding && !isGroundedAnimation) {
            animator.SetBool("isFalling", true);
        }

        if (!isFalling && isGroundedAnimation) {
            animator.SetBool("isLanding", false);
        }
        if (isFalling && isGroundedAnimation) {
            animator.SetBool("isLanding", true);
        }


        // Revolver Aiming part
        if (!isPistolAimingIdle && aimButton) {
            animator.SetBool("isPistolAiming", true);
        }
        if (isPistolAimingIdle && !aimButton) {
            animator.SetBool("isPistolAiming", false);
        }

        bool CheckGrounded()
        {
            RaycastHit hit;
            float distanceToGround = 0.1f; 

            if (Physics.Raycast(transform.position, Vector3.down, out hit, distanceToGround)) {
                if (hit.collider != null)
                {
                    return true; 
                }
            }

            return false;
        }

        IEnumerator CheckGroundedSeconds()
        {
            yield return new WaitForSeconds(0.2f);
         
        }
    }

}