- Home /
How to let death animation play when enemy dies, and stop player from taking damage
I am currently working on a 2D dungeon crawler similar to binding of isaac, and I've tried searching for the answer to this question but can't find anything that works for me. Essentially, once the player's health reaches 0, I want the death animation to play, but it gets locked onto the first frame and I can't quite figure out how to make the animation play out fully in the inspector or via code. What kind of animator settings/script will I have to modify in order to let the animation complete its cycle? I have attached the player health code below (some of it might be a bit clunky or messy, especially because I'm not sure what to put in the IsDead function). Thanks for your help!
public class PlayerHealth : MonoBehaviour
{
public int health;
public int numOfHearts;
Animator anim;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
private GameObject player;
private void Start()
{
anim = GetComponent<Animator>();
player = GameObject.FindWithTag("Player");
}
private void Update()
{
if (health > numOfHearts)
{
health = numOfHearts;
}
for (int i = 0; i < hearts.Length; i++)
{
if (i < health)
{
hearts[i].sprite = fullHeart;
}
else
{
hearts[i].sprite = emptyHeart;
}
if (i < numOfHearts)
{
hearts[i].enabled = true;
}
else
{
hearts[i].enabled = false;
}
}
if (IsDead)
{
anim.SetTrigger("playerDead");
}
}
public void Damaged()
{
if (IsDead == true)
{
}
health -= 1;
if (health < numOfHearts)
{
numOfHearts = health;
}
}
public bool IsDead
{
get
{
return health == 0;
}
}
}
Answer by judahcline · Mar 12, 2021 at 04:05 PM
try removing the is dead bool from the bottom and instead have it up the rest of the variables and then in the update function do: if (health <= 0) {isDead = true;} else {isDead = false;} it will call it faster and be less buggy later on, also i recomend changing most int's to floats (this might not help with the bug but it will make it easier to use later, this is my opinion you can do whatever you want) also i recomend using an animation field (the animation tab on the scene view) so that you can make smoother transitions on the animations while also making some of them automatic (find a tutorial online there are really easy to use i recomend brackeys tutorial on youtube) + can you show us the animation in the animation editor and what happens when you die i dont know if its a script problem
Thanks for the reply! I've attached a screenshot of the inspector, showing the transition from any state to the death animation. I have 6 sprites for the death animation, and when the player dies only the first one is shown - it doesn't cycle through them as intended. I implemented your code, and it definitely made the transition smoother, but I think you're right that the animator settings are the problem. I'm quite new to Unity so I'm unfamiliar with animation. Thanks for your help!
Ok so I unchecked transition to self, and now the animation plays! However, it is on a loop, how do I make it only play once? Thanks!
I solved this problem! I just had to uncheck loop time in the animator settings. Thanks a lot for your time!
Your answer
Follow this Question
Related Questions
Animation not at correct position W/Video 0 Answers
Distribute terrain in zones 3 Answers
Animation Not Playing - VideoExplanation 2 Answers
Animation or programming? 2 Answers
Keep boolean true whilst playing animation, then false 1 Answer