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.

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.