Hello,
I’m fairly new to Unity and this may be a stupid question/problem. I have come across a problem regarding the death animation. Once the enemies health reaches 0, they will play the death animation however they will continue to play it over and over again.
My code:
using UnityEngine;
using System.Collections;
public class Mob : MonoBehaviour
{
public float speed;
public float range;
public CharacterController controller;
public AnimationClip run;
public AnimationClip idle;
public AnimationClip die;
public Transform player;
private int health;
// Use this for initialization
void Start()
{
health = 100;
}
// Update is called once per frame
void Update()
{
if (!IsDead())
{
if (!inRange())
{
chase();
}
else
{
GetComponent<Animation>().CrossFade("idle");
}
}
else
{
GetComponent<Animation>().CrossFade("die");
}
Debug.Log(health);
}
bool inRange()
{
if (Vector3.Distance(transform.position, player.position) < range)
{
return true;
}
else
{
return false;
}
}
public void getHit(int damage)
{
health = health - damage;
if(health<0)
{
health = 0;
}
}
void chase()
{
transform.LookAt(player.position);
controller.SimpleMove(transform.forward * speed);
GetComponent<Animation>().CrossFade("run");
}
void diemethod()
{
GetComponent<Animation>().CrossFade("die");
{
if (GetComponent<Animation>()["die"].time > GetComponent<Animation>()["die"].length * 0.9)
{
Destroy(gameObject);
}
}
}
bool IsDead()
{
if(health<=0)
{
return true;
}
else
{
return false;
}
}
void OnMouseOver()
{
player.GetComponent<Fighter>().opponent = gameObject;
}
}
I think the problem may specifically be around here:
void diemethod()
{
GetComponent<Animation>().CrossFade("die");
{
if (GetComponent<Animation>()["die"].time > GetComponent<Animation>()["die"].length * 0.9)
{
Destroy(gameObject);
}
}
}
,