It’s time for a Power-up!

Rose Owen
5 min readMay 28, 2021

--

Now that we have the basics of a game set up and looking pretty good, let’s start adding in some features like a new Power-up System.

There are so many different ways we can add in a power-up, a different way of shooting, unlimited ammo, faster shooting, faster flying/movement, damage reduction, or even nullification. If you can think of something, you might be able to make it as a power-up.

Let’s start with something basic though, a triple shot. After picking up an item, make the player be able to shoot 3 projectiles instead of 1 for a set time then afterward revert to a single shot. Simple right?

Ehh, yup….

Making of the PreFabs:

We want to be able to spawn in 3 lasers we made before but it would be better to just spawn 1 object, a way around this is to put them into a container / Empty object. First, you will want to move the player to (0,0,0) this will make things easier to line things up.

Next, drag a laser PreFab into the Hierarchy and line it up to where you want it to spawn on the player.

Make sure the laser is 0 on the Z-axis.

Duplicate the laser and move it down to one of the wings/where you want the extra lasers to instantiate from.

Again duplicate the laser we just made and remove the “-” from the X-Axis or place one in if you started from the other side, this should ensure that it’s the same distance on the other side of the ship.

Now to put them in a container. Create an empty game object, you can do this by clicking the create button in the hierarchy or right-clicking in the hierarchy. Before you add the 3 lasers we made, make sure the empty object is at (0,0,0) for its position, this will cause unwanted effects later.

Drag into the project folder to make it into a PreFab, as we did with the normal Laser.

While we are at it, let’s make a super simple Triple Shot Power up. Drag in a Sprite into the Hierarchy, this can get changed at any time.

Add a RigidBody2D and a Box Collider2D. On the RigidBody2D change the gravity scale to 0, and on the Box Collider2D set it as a trigger.

Drag it into the PreFab folder and you’re done… for now.

Coding:

With PreFab made we can now move onto the coding side of things. Looking back at what we want to happen “After picking up an item, make the player be able to shoot 3 projectiles instead of 1 for a set time then afterward revert to a single shot”.

Let’s break this down a little, “After picking up an item” There are 2 parts to this, we will need something to spawn them in, Spawn Manager seems perfect for this. It’s more or less like the Enemy Spawns we made with a name change.

Part 1 Spawning them in.

Part 2 is being able to pick them up. Let’s make a new script for the Power-Ups to attach it to the triple Shot pickup. In the script, when it spawns we want it to slowly move down but if it goes too far, destroy it. If it collides with the player, then call the activate the Triple Shot. Which would look a little something like this.

Remember to attach this to the TripleShotPowerUp we made earlier

make the player be able to shot 3 projectiles instead of 1” in the shoot/fire laser method we can make an if statement that checks if we have the power-up and if we do have it, Instantiate the triple instead of the single. If we don’t have it Instantiate the single. Which would look a little something like this.

For this you will need to make 2 Variables, 1 to store the Prefab for the TripleShot we made before and a Bool to use for if we have the TripleShot or not. Remember to drag the PreFab into the Variable in the inspector, if you can’t see it use [SerializeField] either before or above the PreFab’s GameObject Variable.

“for a set time then afterward revert to a single shot” Something that uses a WaitForSeconds maybe? For this we will need to make 2 methods, 1 to be able to set the _hasTripleShot to true and one to have the cooldown to set it back to false.

TripleShotActive is public so we can have another script call it to start the power-up.

And we all done! Later we can add in an ID system to check what power-up is being given to the player but for just 1 power-up or even 2, this works just fine.

As always test to see if everything is working.

--

--