- Home /
How to destroy an object gradually?
Is it possible to destroy an object gradually? My idea is to use raycasting to hit it and have it dissolve away while you're firing at it. I'm not exactly sure how to go around this, I'm hoping to get smooth satisfying destruction instead of chunky deleting.
Answer by Optrix · Jul 01, 2019 at 12:52 AM
You can kick-off an animation and start a timer, if you like.
In your hit detection, you do the following....
if (Hit)
{
StartCoroutine(Explode());
}
...and you add a function like this on the object that is exploding...
function IEnumerator Explode()
{
gameObject.GetComponent<Animator>().SetBool("Exploding",true);
return WaitForSeconds(2);
GameObject.Destroy(gameObject);
}
This basically does the following...
1) Starts a 'Explode' coroutine - this basically acts independently of your other code and won't 'block' the game.
2) Start the exploding animation
3) Wait 2 seconds (the assumed time for the animation to complete)
4) Destroy the game object.
Instead of an animation, you could instantiate an explosion prefab, enable a particle emitter, change the model or all kinds of other tricks.
Your answer
![](https://koobas.hobune.stream/wayback/20220612204122im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destroy object using mouse click(raycast to detect collision) 2 Answers
Destroy an enemy when it touches an object 2 Answers
Script doesn't work anymore! 2 Answers
Destroy() not working 0 Answers