- Home /
Destroy game object after animation
OK so I've been looking into "Animation Events" not that I understand how they work but it might be what I need for my animation problem. I have a UFO that takes off from a hill when the player gets within a certain distance, but whay I want is about partway thought the fight animation for a small explosion to occure on the side of the UFO and then for it to explode an "Destroy game object" at the end of the animation cycle??
Here is my code so far:
var UFOsound : AudioClip; var escapeDist = 0.4; private var player : GameObject;
function Start(){ player = GameObject.FindWithTag("Player"); }
function Update(){ if(Vector3.Distance(transform.position, player.gameObject.transform.position) < escapeDist){ animation.Play("UFOtakeoff"); audio.PlayOneShot(UFOsound); } }
Everything works great but I can't figure out how to get the small explosion to happen mid flight in the animation, I'm thinking animation event but I've no clue how that works, been looking for an example. Unity's code reference does not really explain it in a way I can understand.
So I guess I would need two animation events, one for the explosion mid flight and another event for at the end of the animation, that is if I understand that part corectly. If I could somehow code in a dead replacement that would be super cool too, but again, my coding knowlede is not that great yet, still trying to wrap my head around how a lot of it works... getting there though, slowly, Oh so slowly :)
Please put a little effort in formatting your code so it's readable. While we usually correct such faults, it's appreciated if you done so yourself.
Hi Statement, yeah I know you edit my stuff for which I am eternaly greatfull, but I do paste it into the space that says "Place Code here" and it still tuns out SNAFU??
Answer by bjennings76 · May 04, 2015 at 09:34 AM
To destory an object from the animation editor...
Create a MonoBehaviour with a simple public function that will destroy the object. e.g.
public class Destroyable : MonoBehaviour { public void DestroyMe() { Destroy(gameObject); } }
Add that MonoBehaviour to the animated object you want to destroy.
In the animation editor, move the animation scrubber to the end of the animation.
Use the 'Add Event' button in the animation toolbar
Select 'DeleteMe' from the function drop-down in the Edit Animation Event dialog.
Now your animation should play, run the 'DeleteMe' function, and destroy the object.
Answer by Statement · Mar 13, 2011 at 04:22 PM
If you're working with imported animations, you can't add animation events using the animation editor since it's read only. What you can do is add the events in scripts instead.
function Start() { var clip = animation["UFOtakeoff"].clip;
// Explode 2 seconds into the clip.
var explode = new AnimationEvent();
explode.functionName = "ExpodeUFO";
explode.time = 2.0f;
// Destroy at end of clip.
var destroy = new AnimationEvent();
destroy.functionName = "DestroyUFO";
destroy.time = clip.length;
clip.AddEvent(explode);
clip.AddEvent(destroy);
}
function ExplodeUFO() { // Create explosion here }
function DestroyUFO() { // Destroy UFO here Destroy(gameObject); }
Actually my UFO animation was done in Unity's animation editor :)
Thanks, will this work with an animation built in Unity's animation editor?