Introducing the First Powerup Into Our Game in Unity — Triple Shot Lasers Part 02

Chris Hilton
3 min readJun 29, 2021

Objective: Part 02 for incorporating our Triple Shot powerup in the Galaxy Shooter game!

Firstly we need to add an important feature for when we collect the powerup, which is to tell the player that they now have the Triple Shot Powerup enabled and for how long they should have it for? This is another fantastic opportunity to dive into coroutines again as we are looking at controlling a timed event!

In our Player script add the following code:

For the above, we have created a new method TripleShotActive() that’s sole purpose is to switch the bool isTripleShotEnabled to true, enabling our powerup when we collect it and switching our lasers from single fire to the Triple Shot as shown below:

At the same time, it is starting the coroutine TripleShotPowerupRoutine() and we are saying keep this powerup going for 5 seconds before switching this bool back to false and going back to single fire.

We also need to add some code to our Powerup script that tells the Player script when they both collide, that the TripleShotActive() method needs to run:

Voila! You should have a functioning Triple Shot Powerup at this stage, let’s give it a test…

As we can see, we are starting off the game with the single laser and when we collect the powerup it switches to the Triple Shot Powerup and it lasts for 5 seconds before reverting back.

Tidying up the Hierarchy

It is now time to tidy up the Hierarchy from the instantiated Triple_Shot_Powerup prefabs. At the moment when the triple shot prefabs are spawned in and when the lasers get above 6 on the Y-axis, they are destroying themselves however, they are leaving behind empty parent game objects and this isn’t desirable.

Solution: Let’s check to see if the laser has a parent attached to it and if it does, let’s destroy both instead of just the laser!

Within the Laser script let’s add the following code:

So here we are checking to see in the if statement, whether the laser game object has a parent. If the parent is NOT equal to null (meaning one exists), then we are going to destroy the parent of the laser game object.

Now in this case, we won’t need to also destroy the laser itself because if we destroy the parent of a child, the child will automatically be destroyed with it.

And lastly, if their is no parent on the laser game object, then simply destroy the laser by itself.

Up Next — Part 3 (Animations)

--

--