A
A
Andrew2022-04-20 21:58:26
2D
Andrew, 2022-04-20 21:58:26

How to add hero death animation to Unity 2D script?

I have a question, how to add the death animation of the hero to the script... The animation itself is ready, .. at the moment I have "Player"_(Hero) in my script. a method is written where lives are taken away (works well). when the number of lives = 0
, Collider2D is disabled, and the hero just falls to the bottom ... And I want the death animation to work instead of disabling the collider, I
wrote a method

(  publuc void Death( )
                                {
                                  anim.SetIneger("State", 4)// номер анимации в Аниматоре
                                 }

(maybe the method is wrong)... I understand that when life = 0, then I insert the Death ( ) method.
but the animation does not work, how to make it work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alalal24, 2022-04-21
@alalal24

- create a new GameObject with your own death animation
- initialize it in the Player class
public GameObject deathAnimation;
- after death, you create an animation in the place of the hero and hide his visibility. delete the hero object after the animation has ended (eg after 1f seconds)
Instantiate(deathAnimation, transform.position, Quaternion.identity);
gameObject.GetComponent().enabled = false;
gameObject.GetComponent().enabled = false;
Destroy(gameObject, 1f);
- pass the GameObject with animation through the inspector to the Hero code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question