Implementing a Simple Power-up System in Unity

Liberty Depriest
3 min readJul 2, 2022

--

Let’s upgrade our space shooter game.

Here we have our simple arcade-like shooter. Then, we were only able to fire one laser at a time. But now we have a powerup called TripleShot which lets us fire three lasers at once. Pretty cool!

Now in order to implement this, we have to plan out what we need:

  1. A new sprite prefab representing a powerup
  2. The two additional laser prefab
  3. A new script for powerup behavior
  4. New collision code and conditional logic code

Onto the sprite, I drag and dropped it into the scene and went ahead and made a script called “PowerUp” on it. This will determine the behavior of the powerup itself. Also, don’t forget to include the 2d collider and rigidbody, and to set “Is Trigger” active and the gravity to 0. Finally, make it a prefab.

scene view
powerup inspector view

Then in our PowerUp script, we implement three behaviors. First, the movement will be travelling downward at 3 meters per second. Second, we check if we went out of bounds, so we destroy this gameObject. Last, we check for collisions with the player, and if so, get the player’s script and activate the powerup and destroy this gameObject.

PowerUp script

Now for our lasers, I duplicated the existing laser prefab into two and made sure the parent’s position is set to defaults (0), and that the children lasers are positioned evenly from one another.

TripleShot prefab

These lasers already have their laser behavior script attached. In it, we define the movement and to check if it goes out of bounds and to destroy itself. We also check if the TripleShot empty gameObject exists, we can only be done through “transform.parent” (and using transform.parent.gameObject gives us a nullreference exception) and to destroy that parent.

Laser script

So how do we activate these extra lasers? In our PowerUp script, we call to the Player script and get its TripleShotActive method. In here, we enable the bool “isTripleShot” to true and start the cooldown coroutine. And in the FireLaser method, we check if we enable extra lasers, and if we do, we instantiate the prefab we just created.

Player script
TripleShot prefab in inspector

And this is what we get:

And that’s it! Now we got a 5 second upgrade on our lasers. Of course, this can go with any other powerup, but additional lasers are just an example. Hope this was helpful, and have a great game-dev day!

--

--