Day 17: Powerups! Now it’s a Real Video Game

Like any good retro space shooter game, we really need a powerup! Lets make something that allows the player to shoot three lasers instead of just one.

First, we can create a Triple_Shot prefab by arranging the lasers like this, and then putting them inside an empty game object. This will be the prefab that is created when we want the player to shoot three lasers.

In the Player script, lets add a boolean variable that stores whether or not we should use the triple shot, and a GameObject that stores the prefab that we will create. Afterwards, make sure the _myTripleLaser variable is assigned in the inspector.

Then we can update the behavior in the Player script. When firing, we can add an if statement to determine whether or not to use the triple shot.

When we run the game, it works….. kinda.

So why isn’t the laser in the right place? Prefabs can be a little particular about their position when created, so it looks like we need to adjust the lasers inside the new triple shot prefab.

This happened because I didn’t ensure that the empty Triple_Shot game object was at 0,0,0 before adding lasers to it.

Since we adjusted them in the scene, we need to make sure that the changes are made to the actual prefab as well. Thankfully there’s the handy Overrides drop down menu in the Inspector. We revert the changes to the Triple_Shot’s transform (making it a child of the Player had scaled it down, which we didn’t want) but apply the changes to each of the Lasers in it.

Now we have some real working firepower!

Now let’s create a powerup that causes the player to switch to using triple shot while the game is running. Here we have a new Triple Shot Powerup, complete with a circle collider that has been set to trigger.

We also need a new Powerup script so that it can move, be destroyed, and collide with the player. Just like with enemies, we can use OnTriggerEnter2D to detect a collision, and get the Player component and tell it to start using triple shot.

We have to make a few adjustments to the Player script as well. _useTripleShot should default to false, and we need to write a new public method StartTripleShot so the powerup script can use it.

Just like that, we can collect the powerup and turn one laser into three!



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
Blake Zoeckler

Blake Zoeckler

I’m a passionate and talented software engineer seeking an opportunity in game development.