- Home /
SetTrigger not play animation
Hello. A have a problem with condition. When I kill enemy supposed to trigger by condition an animation (fall enemy )But it doesn't happen.I cant understand why. Here is my code. Appreciate any help.Sorry for bad english.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyHealth : MonoBehaviour { [SerializeField] private int startingHealth = 2; [SerializeField] private float timeSinceLastHit = 0.5f; [SerializeField] private float dissapearSpeed = 2f; [SerializeField] private AudioClip diesfx; [SerializeField] private AudioClip hurtsfx;
private AudioSource Audio;
private Animator anim;
private UnityEngine.AI.NavMeshAgent nav;
private bool isAlive;
private Rigidbody rigidBody;
private CapsuleCollider capsuleCollider;
private bool dissapearEnemy = false;
private int currentHealth;
private float timer = 0f;
public bool IsAlive
{
get {return isAlive; }
}
// Start is called before the first frame update
void Start()
{
Audio = GetComponent<AudioSource>();
anim = GetComponent<Animator>();
nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
rigidBody = GetComponent<Rigidbody>();
capsuleCollider = GetComponent<CapsuleCollider>();
isAlive = true;
currentHealth = startingHealth;
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if (dissapearEnemy)
{
transform.Translate (-Vector3.up * dissapearSpeed * Time.deltaTime);
}
}
void OnTriggerEnter(Collider other)
{
if (timer >= timeSinceLastHit && !GameManager.instance.GameOver)
{
if (other.tag == "PlayerWeapon")
{
takeHit ();
timer = 0f;
}
}
}
void takeHit ()
{
if (currentHealth > 0 )
{
Audio.PlayOneShot (hurtsfx);
anim.Play ("Hurt");
currentHealth -= 10;
}
if (currentHealth <= 0)
{
isAlive = false;
KillEnemy ();
}
}
void KillEnemy ()
{
capsuleCollider.enabled = false;
nav.enabled = false;
anim.SetTrigger ("EnemyDie");
rigidBody.isKinematic = true;
Audio.PlayOneShot (diesfx);
StartCoroutine (removeEnemy());
}
IEnumerator removeEnemy ()
{
yield return new WaitForSeconds (4f);
dissapearEnemy = true;
yield return new WaitForSeconds (2f);
Destroy (gameObject);
}
}
Your answer
Follow this Question
Related Questions
How check if an animation trigger is playing? 1 Answer
Why does my climb stairs animation not play? 1 Answer
How to trigger animation on a different object? 1 Answer
Play parent animation without affecting the child animation 1 Answer
Should we check animation condition in Update or FixedUpdatE? 3 Answers