Powerups are a great way to add some more fun to your game. In this example you are going to add a triple shot powerup to the game. Start by dragging the powerup sprite into the Hierarchy to create the object. Adjust the size as needed and set the layer to foreground. Add in either a box collider 2D or a circle collider 2D. Whichever you feel fits best. Check the isTrigger box. Add a rigidbody 2D and set the gravity scale to 0. Now create and add a powerup script to the object. By now this should all be fairly standard procedure for creating a new object. You can now move it to your prefab folder.

Next, you need to setup the script. Getting the powerup to move down the screen is just like having other objects like the enemy and the laser move. You will need a speed variable and will then use it in the tranform.Translate function. Then choose a point when it has left the bottom of the screen to destroy it. See the code below.

For the on trigger enter event you will want to check the tag and make sure it is the player. If it is you need to get the player component. Here you will call a function from the player to activate the triple shot. This will be covered further down. After the triple shot is activated you will need to destroy the object. See the code below.

Now it’s time to create the triple shot. To do this create an empty object and add three lasers to it. Space the lasers as needed to fit your spaceship. Move the triple shot to the prefab folder.

Now go to the player script. First add a couple of variables. One to track if triple shot is active and one to reference the triple shot prefab. Make sure you connect the prefab in the editor.

Next, update the FireLaser function to check if triple shot is active. If it is active instantiate the triple shot instead of the regular laser. See the code below.

Now update the player code to add the function you called in the powerup script. In it first set the triple shot to active. Then start a coroutine that will used to deactivate the triple shot. Now create the IEnumrator function that will be used to power down the triple shot. Set it to wait for 5 seconds then set the triple shot to not active. See the code below.

Now the triple shot powerup is working. There is only one problem, it leaves lots of empty triple shot objects in the hierarchy window. You can fix this in the laser script by checking if the laser has a parent and if it does destroy the parent.

Now you are powered up!

Good Luck Adventurer!

-Chris