35 lines
788 B
C#
Raw Normal View History

2023-06-18 13:37:19 +02:00
using UnityEngine;
public class FallingDetection : MonoBehaviour
{
private CharController charController;
private Rigidbody2D playerRigidbody;
private bool isFalling;
private Animator animator;
private int counter = 0;
2023-06-18 13:37:19 +02:00
private void Start()
{
animator = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody2D>();
charController = gameObject.GetComponent<CharController>();
}
2023-06-18 13:37:19 +02:00
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);
}
}
2023-06-18 13:37:19 +02:00
}