- Home /
can someone show me how to play a death animation when a the game object is destroyed (javascript)
Basically i need help with getting my enemy in game to play a death animation when the game object is destroyed.
Basically you would first play the animation and then destroy the gameObject. You can write a custom destroy method (function) that plays an animation and then destroys the GO as soon as the animation finished. Tell me if you need any help with the code :) Do you have some scripts already?
Answer by InfiniBuzz · Jun 17, 2013 at 05:11 PM
try this :)
#pragma strict
var Health = 100;
var animDie : AnimationClip; // Drag your animation from the project view in here (to inspector)
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
animation.Play(animDie.name);
Destroy(this.gameObject, animDie.length);
}
hope it helps
Answer by SubatomicHero · Jun 17, 2013 at 08:28 AM
In your script somewhere you would call Destroy() like so:
// if this script is attached to the object to destroy
animation.Play("animation name goes here");
Destroy(this.gameObject, animation.time);
#pragma strict
var Health = 100;
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
animation.Play("dead");
Destroy(this.gameObject, animation.time);
}
thats the code btw i get an error saying." 'time' is not a member of 'UnityEngine.Animation' yeah i'm pretty new to coding.thanks for helping
thanks man yeah i'm pretty new to code do i put that where the animation.time is?
Answer by branek42 · Oct 10, 2013 at 09:04 PM
Help me pls script is not work send me instructions gagac@centrum.sk
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I need help with adding my character controller to a Character model 2 Answers
How to I vary animations based on proximity? 2 Answers
Player colliding with animations 0 Answers
Animation Keeps Looping 2 Answers