Adventures in GameDev with GameDevHQ! Day 28: Creating enemy explosions!
Ah yes, the eternal question: EXPLOSIONS?
Let’s face it. Explosions are cool! Michael Bay has made an entire career out of them and what’s a videogame without explosions? A pretty dull one, I’d measure! At least Mr. Torque would agree.
In our Galaxy Shooter game, we’ve actually been given sprites for a really dang cool explosion so our job is to put them into the game.
In our example, we need to shoot an asteroid and make it go boom! Let’s see what we have at the moment.
BORING! Let’s Michael Bay this and begin by putting the first frame of the sprite onto the scene.
As in animating the PowerUp, open the animator window and create a new animation called EXPLOSION_anim. I’ve already created a folder called EXPLOSIONS. You may want to do the same just to keep things organized.
Next drag all the frames of the explosion into the animation window.
Looks good! Set the sorting layer to the foreground and level 1 just to be sure it gets placed on top of everything else.
Don’t forget to rename it to Explosion and prefab it.
One more thing, did you notice the animation looped when we played it? We gotta nip that in the bud, there are a couple of places we can access the looping checkbox that we need, but let’s go directly to our animation folder where we saved the explosion animation and click on the explosion anim. to the right in the inspector, you’ll see a checkbox called ‘Loop Time’. Just check it to turn it off and we’re set!
OK! We’re ready to make the asteroid explode! Let’s make room for it in the Asteroids script by creating a handle for it. We’ve done this so many times by now, we’re going to get to the point I can just tell you to do it and you’ll automatically know but for now, here’s the code:
Next, we’ll drag the actual explosion into it in the inspector.
Now that we have a handle on our explosion, we can instantiate it when the Asteroid blows up! The asteroid is already equipped with a BoxCollider2D set to trigger so we’ll use OnTriggerEnter2D. We did that with our lasers so it’s easy peasy!
Let’s watch our masterpiece!
It’s perfect as it is, but for a little added realism, you may want to keep the asteroid on the screen as the explosion begins instead of it disappearing immediately. All you need to do is just tell Destroy to wait a bit.
Tomorrow we’ll talk about adding damage to the player by adding VFX sprites to it. Stay tuned!