- Home /
Animation play when dead
How do I make an animation play when a person dies?
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width /2;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth <1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth /(float)maxHealth);
}
}
i have yet to delve into the animation part of unity yet but normally, you do all the 'death' related thing once your object's status meets your death condition.
for your case i'll assume your player is dead once curHealth is less than zero, so you can do your logic when "if(curHealth < 0)" is run.
Answer by lighting · Dec 21, 2012 at 12:55 AM
My proposal is - in Mecanim create state called Dead which will have "dead" animation assigned to it. Then make transition to this state, make curHealth as transition parameter and set transition's condition following: curHealth Less 1.
Yes, This can be done in 4.0 very easily. Thanks for re$$anonymous$$ding that lighting.
Answer by Benjamindavid · Dec 20, 2012 at 02:02 PM
declare an Animation as anim and then play the death animation public anim : Animation;
then in update play the animation when the player health = 0 using
anim.Play("DeathAnimation");