It’s only been a few short weeks but the 2D Space Shooter is really starting to feel like a real video game! In past articles we have discussed adding prototypes, basic movement and simple enemies to the game, but what game would be fun without some sorry of boost?
Today we add the first of our powerups to the game, the “Triple Shot”. We start with a simple 2D sprite and drag it into the inspector. If you are interested in following along with the excellent GameDevHQ courses you use the same sample assets we have been using for the past few weeks. Check out GameDevHQ for details about joining the program.
Once we have the sprite on screen we need to start building out the logic behind the new power up. Let’s start with creating a new C# script and add it to our power up sprite. I have called mine powerup.cs because I plan to build a full array of powerups into the game in a modular fashion down the line.
Above is the basic logic we need to make the new power up move down the screen, destroy itself once it goes offscreen and handle a collision trigger with the player. Any other collisions are ignored. You will notice the cast of other to a player variable, this is so we can enable the power up down the line. In the future we will deal with this a different way but for now a direct call to EnablePowerUp() will work.
We need to do a little prep work on the player script to deal with this new awesome powerup. So let’s have a look at the new additions.
First we add two new serialized variables to the player script to deal with the new powerup system. The first will determine if the powerup is enabled and is set to false by default. This is serialized for testing only and in the future will have it’s serialization removed. The second is a private GameObject that will hold our TripleShot prefab that we will create momentarily.
Next we add some logic to our fire method to check if the powerup is enabled. If it is we will instantiate that instead of the default laser. The player still needs a little more logic though. Remember the EnablePowerUp() method we called in the powerup script? We need to implement it.
We add two small methods to deal with the enabling of the powerup. First we add the public void EnablePowerUp() method and use it to set the powerup enabled to true, then we start a new coroutine to keep the powerup enabled for five seconds before turning it off.
Now we have all the code we could want. Our powerup game object has been created but currently our power up is more like a power down because we don’t actually have a power up built for the player to use.
So we need to create a new prefab for the powerup. We start by adding an empty game object to the hierarchy and rename it to TripleShot. Then we add one of our laser prefabs to the empty object. Now we can either add two more lasers, or duplicate the one we have added. Finally we need to drag the new object into our prefabs and position the other two lasers.
There are several ways to position the second and third lasers, but it is best to set them at the same exact Y pos, and their X pos should also have the same absolute position, with the left laser being negative and the right laser being positive. Save yourself some headache and work with your player at 0,0,0 when building the new weapon.
Finally we need to do a little bit of cleanup on our laser script to make sure we don’t leave some empty parent objects laying around in our hierarchy. To handle this we update the Laser behavior script so that it checks for and destroys the lasers parent if it exists.
Now we have a fully functioning Triple Shot powerup with the basic bones for a full on modular powerup system that we will build in the future. Once we get some more enemies, bosses and animations going we will have a really solid base game. Check back tomorrow as we animate some sprites to give some more life to the game.