fgm24/Assets/Scripts/Enemy/EnemyAnimationHandler.cs

45 lines
1.0 KiB
C#
Raw Normal View History

2024-02-03 10:52:24 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-02-04 02:26:11 +01:00
using UnityEngine.AI;
2024-02-03 10:52:24 +01:00
public class EnemyAnimationHandler : MonoBehaviour
{
Animator animator;
void Start()
{
animator = GetComponentInChildren<Animator>();
2024-02-03 21:31:14 +01:00
if (animator != animator.enabled)
animator.enabled = true;
2024-02-03 10:52:24 +01:00
2024-02-04 00:08:25 +01:00
GetComponent<HealthComponent>().OnHealthZero.AddListener(EnemyDie);
2024-02-03 10:52:24 +01:00
}
2024-02-04 00:08:25 +01:00
void EnemyDie()
{
2024-02-04 02:26:11 +01:00
GetComponent<Collider2D>().enabled = false;
GetComponent<NavMeshAgent>().enabled = false;
2024-02-04 00:08:25 +01:00
StartCoroutine(AnimationDie());
}
IEnumerator AnimationDie()
{
Strangle();
yield return new WaitForSecondsRealtime(1f);
Die();
while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1f)
yield return new WaitForEndOfFrame();
Destroy(gameObject);
}
2024-02-03 10:52:24 +01:00
public void Strangle()
{
2024-02-03 21:31:14 +01:00
animator.SetTrigger("Die");
2024-02-03 10:52:24 +01:00
}
public void Die()
{
animator.SetTrigger("Die");
2024-02-04 00:08:25 +01:00
2024-02-03 10:52:24 +01:00
}
}