I try to make the slime gone after is hp is 0 or lower than 0 but when i attack it, it didn't gone but the hp keep going down by - I try to change the Destroy code but it still didn't work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
Animator animator;
public float Health {
set {
health = value;
if(health <= 0) {
Defeated();
}
}
get {
return health;
}
}
public float health = 1;
private void Start() {
animator = GetComponent<Animator>();
}
public void Defeated(){
animator.SetTrigger("Defeated");
}
public void RemoveEnemy() {
Destroy(gameObject);
}
}
CodePudding user response:
You don't call RemoveEnemy()
. You need to wait to play the 'Defeated' animation, then call RemoveEnemy()
.
If you know the animation's length, you can change Defeated()
to a coroutine and wait for it, like:
private IEnumerator Defeated()
{
animator.SetTrigger("Defeated");
yield return new WaitForSeconds(_defeatedAnimLength);
RemoveEnemy();
}
Alternatively, you can set a trigger in the animation, which will call RemoveEnemy()
from the animation.