- Home /
Play animation on death?
Hello,
i have done some tutorials to creat my first game.
To finish the project i have to add a menu that pops out when the player dies. Is it possible to play the "menu pop out"animation on collision if the animation is a "animator" animation?
I got this code:
void OnCollisionEnter2D(Collision2D collision) {
if (godMode)
return;
animator.SetTrigger("Death");
//use other code to start animation. setTrigger will stop "death"animation
//animator.SetTrigger("DeathMenuOpen");
dead = true;
}
}
I tried to use the animator but if i use the "set.trigger()" code, the death animation will end.
I see 2 problems here:
Why are you using an animator to open a menu?
Why are you using the SA$$anonymous$$E animator to play a death animation (on your character probably) and open a menu?
Why not use another object for your menu and have that one do whatever you want on death? Different animator and all.
i want to open the menu with an animation and yes it is better to use an other animator for the menu but i want to start the animation when the player dies :/
You can do it in a lot of ways, keep a reference to the other object and call a method on it, for example. Or you could find the menu object and send it a message.
The way I'd do it is have a Level$$anonymous$$anager object that handles level started/level won/level lost/etc events. When the player dies you tell the Level$$anonymous$$anager what happened and the Level$$anonymous$$anager will handle all other things (call the menu object to open it, etc).
$$anonymous$$y menu has the animation "Bird$$anonymous$$enuOpen" and my player has a script to call the animation but it does not work.
Error: "The animation state Bird$$anonymous$$enuOpen could not be played because it couldn't be found! Please attach an animation clip with the name 'Bird$$anonymous$$enuOpen' or call this function only for existing animations.
my script attached on the bird to call the menu.
using UnityEngine;
using System.Collections;
public class Bird$$anonymous$$enuOpen : $$anonymous$$onoBehaviour {
bool birdDeath = false;
public GameObject otherObj;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D collision) {
birdDeath = true;
if (birdDeath == true) {
otherObj.animation.Play ("Bird$$anonymous$$enuOpen");
//animator.SetTrigger("Death$$anonymous$$enuOpen");
Debug.LogError("DEATH $$anonymous$$ENU OPEN");
}
}
}
Answer by GreenTee · Mar 19, 2015 at 10:56 PM
Problem solved! I had to make sure the animation is in legacy mode and the object with the animation is assigned in the inspector.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public GameObject MenuObj;
void OnCollisionEnter2D(Collision2D collision) {
//play animation of the menu when player collides with something!
MenuObj.animation.Play ("BirdMenuOpen");
}
}
sometimes it is to easy ... :7
Answer by alok-kr-029 · Mar 19, 2015 at 09:18 AM
Create the menu open animation and put the animation on the gameobject and call the animation using animation.Play
you dont need animator
yea but the menu has to open when the player dies and i don't know how to start the animation when the player dies.
Player death > start animation of other object
Your answer
![](https://koobas.hobune.stream/wayback/20220613190551im_/https://answers.unity.com/themes/thub/images/avi.jpg)