- Home /
Character disappears when dies.
My character just disappears when he dies, i was wondering how to play the "die animation" when he dies instead of just disappearing?
Any help would be fab thanks! :)
The code i have is:
function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject);
animation.Play("die");
}
Answer by Dave-Carlile · Nov 29, 2012 at 05:43 PM
It's disappearing because you're destroying it. Remove the Destroy call.
but it doesn't die with out it? i don't have a clue how else to get it to die (i know that sounds stupid but i've never used this program before :/)
Destroy() will delete whatever object you give it, the line 'animation.Play("die");' is never reached.
There are several ways you could do what you want. You could either add a variable like 'bool alive' that is true when the player is alive, and only let the person playing the game control the character when it is alive.
Update() { if (alive) { //Put your controls/code to run while character is alive here } else if (alive == false) { animation.Play("die"); }
Or you could make another version of you player that only has a death animation on it with no player script and when your player dies spawn the deathAnimationPlayer where your Player was and then destroy your player.
You would make a small script for the deathAnimationPlayer that would only need 'animation.Play("die");' in the Start() function.
function OnTriggerEnter (other : Collider) { GameObject temp = Instantiate(deathAnimationPrefab, gameObject.transform.position, gameObject.transform.rotation); Destroy(other.gameObject); }
Answer by Chronos-L · Nov 30, 2012 at 08:55 AM
If you want your object disappear after the animation, modify your code to:
`function OnTriggerEnter (other : Collider) {`
animation.Play("die");
}
private void Kill() {
//Destroy your player or whatever you
//want to happen at the end of your animation
}
You can then attach Kill() in the end of the [die] animation via animation event
And the Kill() function will be called in the end of the animation (or at the frame you have assigned the function to)
Thanks, i tried to get this to work but i don't really have a clue how to add that animation event thing :/ (literally barely know how to do anything properly on unity haha)
Answer by Maulik2208 · Nov 30, 2012 at 01:00 PM
I think you should use Destroy(other.Gameobject,10); this code will destroy your gameobject after 10 sec so in that 10 sec you play the animation.Play("Die"); for better visualization you can use your animation time's lenght +1 or +2 sec.......means if the animation is of 10 sec then destroy at 12......by using Destroy(other.Gameobject,12);.......Here 12 represent time in sec to wait before destroy the object Hope This will work for you........Enjoy
Like this?:
function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject,5); animation.Play("die"+3);
}
The delay seems to be working, but the animation still isn't playing before the character disappears?
Is it a problem with the animation itself? If you comment out the Destroy function call does the animation play?
(In case you don't know, you can comment out a line by adding '//' at the beginning and the compiler won't read any code from that line, good for documenting what does what and temporarily removing code)
The animation seems to work, but it doesnt fully play. when the character bumps in the cube (the trigger that will kill the character) the animation starts to play, but then just stops the character will stand normally and then die after the 10seconds.
Yeah i've been commenting stuff. (this is the first time i've ever wrote code for a game so i'm pretty hopeless at getting anything to work properly haha)
In that case when you split the animation into clips you may not have given it every frame. Look at the animation file and make sure the frame numbers line up to the clip frame numbers
Your answer
Follow this Question
Related Questions
Death on impact with water 1 Answer
AI Awareness 2 Answers
Death script problem 2 Answers
Scene not restarting after player death 2 Answers