Death trigger panel animation
I have a scripts that attach to a enemy that once collision happens with our player the game restarts. I have a panel with an animation with in it, i would like this animation to be triggered once my player dies. Could somebody help me make a c-sharp death script the triggers the panel animation. Also if you could include a sound for when the player dies that would be much appreciate Here's my death script:
using UnityEngine;
public class death : MonoBehaviour
{
// The force which is added when the player jumps
// This can be changed in the Inspector window
public Vector2 jumpForce = new Vector2(0, 300);
// Update is called once per frame
void Update ()
{
// Jump
if (Input.GetKeyUp("space"))
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
}
}
// Die by collision
void OnCollisionEnter2D(Collision2D other)
{
Die();
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
}
Answer by itsharshdeep · Aug 31, 2015 at 02:17 PM
just add a reference of a animator & audiosource
public Animator animator;
public AudioSource audioSource;
void Start(){
animator = gameObject.GetComponent<Animator>();
audioSource = gameObject.GetComponent<AudioSource>();
}
// Add below lines
// where you want to play that animation + sound :-
animator.SetTrigger( "Play");
audioSource.Play();
Thank you, i'll try it out and tell you if it works.
Also do I add this to my death script or my player. @itsharshdeep
This code will be present there where the audioSource & animator are attached
Your answer
Follow this Question
Related Questions
Play Animation on key pressed 0 Answers
How to detect collision of an animating character? 2 Answers
How do I check if the x position of my object is 0? 1 Answer
First person shooter : 2d sprites 1 Answer