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