Adding Our First Power Up

The Triple Shot!! Three times the fire power!!

Daniel Jennings
4 min readMay 1, 2021

Objective: Create our first power up which modifies our attack

Create the TripleShot Laser Prefab

To start, let’s drag a Laser prefab into the scene. Line it up how you want as the central laser. Then duplicate it and set up one of the back lasers. Once you have one back laser aligned as you like duplicate it and use the Move Tool (W) to keep the y alignment and only adjust the x value.

To keep the lasers together create an empty GameObject and move the three new lasers under the new object and rename it as TripleShotLaser.

TripleShot Laser behavior

Since the lasers already have a movement method attached we do not need to do much to the TripleShotLaser. Create a method that checks for children. Once all of the lasers have destroyed themselves the TripleShotLaser will have no more children and will destroy itself.

Create the PowerUp Prefab

Start by dragging a sprite into the hierarchy. This will create a GameObject for you. Rename the GameObject to TripleShotPowerUp. We need to add a Rigidbody2D, C# script, and a Collider2D. In this case we will add a Circle Collider 2D.

PowerUp Prefab behavior

We want the prefab to fall similar to an enemy so we will need to add a movement method. If the Player misses collecting the prefab we will need to destroy the prefab once it goes off the screen.

The last step in setting up our script is to detect collisions. For this we will use OnTriggerEnter2D and Collider2D with tag comparison.

Over in the Player script we setup our SetPowerUp so that the other power ups can use it as well. The TripleShotPowerDown coroutine controls when to turn the powerup off.

We also need to control what we are shooting out of the Player. I used a ternary expression to control which prefab to instantiate in the FireLaser method.

Animating the PowerUp

Now to the fun part!! Open the Animation window and dock the window where you like.

Click on the Create button and it will take you to the Animation tab.

Click on Record and the select all sprites you want to include in the animation and drag them over to the Dopesheet. Clicking play on the animation tab will show you the animation in its current state.

Add the PowerUp to the SpawnManager

Now that our power up is ready, let’s add it to the SpawnManager. We will need a GameObject parameter with a developer null check. We then create a coroutine with a random timer to spawn power ups.

As the name may imply, if we can use this method for other power ups in the future, we will try.

--

--