Nerd For Tech
Published in

Nerd For Tech

Animating Sprites in Unity

In this article you’ll learn about creating an explosion effect and how we can activate it when an asteroid gets destroyed.

Objective:

To trigger an explosion after an asteroid has been destroyed.

Necessities:

  • “Asteroid” Script
  • A sequence of sprites used for the explosion animation.

Logic:

Preparing the Explosion VFX

→ Create an empty GameObject “Explosion” that will represent your explosion sequence. Open the Animation Window and make sure to have the Explosion GameObject selected.

→ Create a new animation file, Shift-Select all of the frames we want to display in the sequence, respectively starting with the first frame ending with the last frame. Drag them onto the Timeline.

→ You’ll see the Explosion GameObject now has an Animator Component on it, don’t forget to disable “loop” for the explosion animation so we only play it once. Drag it into a new VFX folder to make a prefab of it and assign it to the foreground layer.

→ Create a small script attached to the Explosion prefab that will simply be responsible for destroying it after a small delay.

Explosion Prefab

Modifying the Asteroid Script

Now we can instantiate this explosion prefab at the position of the Asteroid, immediately after the Asteroid destroys itself. I’ve added an offset on the Z-axis for the Explosion effect since we are mixing 2D with 3D. If you’re new to instantiating, you’re welcome here, to have a look at its basic principles.

Note:

Again, there are a few possibilities to achieve the desired effect. For instance you could play an animation on the Asteroid Prefab that is triggered in OnTriggerEnter() and then destroy the prefab after the animation has played, you could replace the GameObject with a different one representing a “broken” state, you can start playing with the Collider and Renderer components. Or, you could create a prefab for the explosion on its own and instantiate it when triggered, like we are opting for in this article.

Previous | Next

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gert Coppens

Gert Coppens

Software Engineer — Unity Game and Application Developer