- Home /
How can I play an explosion animation?
I want to instantiate an explosion prefab, so an explosion animation (created as an 8-frame animation) is played, then destroy the instantiated object. I can not achieve my goal as I don't know how to destroy the explosion object once the animation is finished.
All tutorials I've seen related to explosions instantiate a particle effect. I'd like to know if this is the only way to do it (or the only PROPER way to do it) or if what I'm trying makes any sense.
In case I should create a particle effect, what should I do to get the same result as in a "standard" animation frame by frame?
how do you play animation? is it videotexture or some tool to play spritesheet?
you could swich off lopping (hwo to do this depends on how do you play animation) and then just check if animation finished in Update (and destroy object when finished).
Answer by hav_ngs_ru · Jan 19, 2015 at 04:26 PM
alternative way:
public explosionTime; // set it in inspector
private void Start() {
Invoke("DestroyMe", explosionTime); // shedules derived call
}
private void DestroyMe() {
Destroy(gameObject);
}
You don't really need to use Invoke as you can just use the Destroy function with a delay:
Destroy(gameObject, explosionTime);
Indeed, the Destroy call can be scheduled and it works like a charm. Thank you all!
Answer by chariot · Jan 19, 2015 at 04:23 PM
private expTimer : float;
public expTime : float;
function Update () {
expTimer++;
if (expTime < expTimer/60){
Destroy(GameObject);
}
}
JS
In inspector set expTime
This counts number of frames and not time, which will not result in consistent ti$$anonymous$$g as it will depend on the current frameRate.
So what?) Just for 60 fps, but, thnx for new way, i didnt know that :)
Answer by SeasiaInfotechind · Jan 20, 2015 at 08:59 AM
Hello,
I suggest you to make a prefab by dragging the gameobject into the prefab folder and instantiate it where you want to run the explosion Animation. After the instantiation of prefab to delete the animation by appling "Destroy(explosion(Clone))". Because the prefab is now instantiating in the form of clone.
Link : http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Hope this will help you.
Thanks
Your answer
Follow this Question
Related Questions
How can I emit animated prefabs from a particle system? 0 Answers
Setting animator parameter on a single instance of a prefab sets the parameter for all instances 3 Answers
Partical System is not visible 1 Answer
Animations on Instantiated Prefabs not working 0 Answers
2D Animation does not start 1 Answer