- Home /
how i can stop the animator to play trigger animation?
hey guys , i have an enemy script that detect the distance to the player and do actions....so when the enemy health is <= 0 i want him to die ..if(health<=0){ animator.SetTrigger("die")}, the problem i have is the enemy is keep playing die animation, i know the problem is the enemy is keep checking the distance to the player but i dont know how to solve this here is the script: using UnityEngine; using System.Collections;
public class enenyscript : MonoBehaviour { public Transform player; Animator anim; public float inrange; public CharacterController cc; public float speed; public int health; public float destroytime;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
if(inRange())
{
transform.LookAt(player.position);
anim.SetBool("attack",true);
}
else if(!inRange())
{
Chase();
anim.SetBool("attack",false);
}
}
bool inRange()
{
if (Vector3.Distance(transform.position, player.position) <=inrange)
{
return true;
}
else
{
return false;
}
}
void Chase()
{
transform.LookAt(player.position);
cc.SimpleMove(transform.forward * speed);
anim.SetFloat("Speed",1);
}
public void gethit(int damage)
{
health = health - damage;
if(health<0)
{
health = 0;
anim.SetTrigger("die");
}
}
void OnMouseOver()
{
playercombat.opponent = transform;
}
}
Answer by cmonroy · May 14, 2016 at 06:02 PM
Check your update function...
if(enemy == alive) { // do your stuff.. } else { // do nothing, since the enemy is dead... }