Let’s Shoot more Lasers!

Credits: Star Wars Rebels

What is a space shooter game without power-ups? Not very fun to be honest. As you are playing the game more and more enemies are spawning and maybe you’re not destroying them fast enough and now you’re thinking to yourself “a nice power-up would be great right about now”. And that’s exactly what we’ll be covering in today’s article

The Fabled TripleShot

Our first power-up will be firing three lasers and we’ll call it the “Triple Shot”. In today’s article, we’ll be revisiting the mechanics we’ve covered in previous articles like creating prefabs which we can spawn in later.

We’ll start by creating a new empty object in the Inspector and call it “TripleShot”. Next, you’ll drag in the “Laser” prefab from the prefabs folder into the new TripleShot object in the inspector. We’ll be duplicating the laser 3 times because this power-up will fire three lasers.

Select your player and make sure the x and y transform is set to 0 so that we can move the lasers to the correct positions. For the next part, you’ll want to make sure the Scene view is active and check that the TripleShot prefab is also 0 on the x and y transform and repeat the steps for each laser. Now we can move the individual lasers to where we want them

At this point, we can override the changes we made to the prefab and delete the object from the hierarchy.

Let’s code the logic

We’ll be working on two scripts today but first, we’ll be working in the “Player” script where we’ll be coding the logic to fire the TripleShot.

We’ll be changing a few things in the “Fire()” Function. We’ll be introducing an “if” check to see if the TripleShot is active and if so we’ll be firing the TripleShot prefab instead. If the TripleShot is not active, then we’ll use an else statement to fire the regular lasers. We’ll be using a bool which we’ll serialize to test the logic.

Script Referencing

When we want to access a variable in another script or perform actions in other scripts, it’s never a good idea to make variables like health, power-ups or lives public for security and other reasons. What we do is create public functions and set up the logic in those functions and then reference to that function from other scripts. And we’ve been doing that in our scripts but I never really covered that in my articles.

In my player script, I’ve created a public function which I can refer to in a future power-up script. But for now, what this function does is it activates the bool we’ve created and called a cooldown CoRoutine.

We’ve completed the TripleShot power-up setup and in the next article, we’ll be creating a PowerUp script that will give the player the TripleShot when we interact with it and we’ll also be creating another spawn CoRoutine in our “SpawnManager” Script to periodically spawn power-ups.

As always the code can be viewed here. Thanks for reading.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store