using UnityEngine;

public class FallingDetection : MonoBehaviour
{
	private CharController charController;
	private Rigidbody2D playerRigidbody;
	public bool isFalling;
	private Animator animator;
	public int counter = 0;

    private void Start()
    { 
	    animator = GetComponent<Animator>(); 
	    playerRigidbody = GetComponent<Rigidbody2D>(); 
	    charController = gameObject.GetComponent<CharController>();
    }
    
    private void Update()
    {
		Vector3 velocity = playerRigidbody.velocity;
		
		if (!charController.grounded && velocity.y < -0.1f) {
			if (isFalling || counter++>90) {
				isFalling = true;
				animator.SetBool("IsFalling", true);
				counter = 0;
			}
		} else {
			isFalling = false;
			counter = 0;
			animator.SetBool("IsFalling", false);
		}
	}
}
