2023-06-21 16:13:04 +02:00

35 lines
788 B
C#

using UnityEngine;
public class FallingDetection : MonoBehaviour
{
private CharController charController;
private Rigidbody2D playerRigidbody;
private bool isFalling;
private Animator animator;
private 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);
}
}
}