Adding a Mega Laser Powerup to Our 2D Galaxy Shooter in Unity!

Chris Hilton
Geek Culture
Published in
4 min readAug 24, 2021

Objective: To implement a new weapon type (mega laser) that is available to use once the player has collected the powerup.

We have recently implemented the new ammo refill and health refill powerup, it is now time to give the player another weapon in the form of a mega laser that is going to be devastating for the enemy!! Let’s dive into it…

Creating the Mega Laser Weapon Prefab

Let’s take our existing laser sprite and make some changes to it, turning it into a giant laser. Let’s have a look at the end product so we know what we are working towards:

Drag the laser sprite image into the Hierarchy window and rename to ‘Mega_Laser”. Let’s now have a look the components:

  1. Let’s change the Tag to ‘Laser’.
  2. In the Transform component let’s change the Position and Scale to our preferred size.
  3. In the Sprite Renderer component, set the sorting layer to ‘foreground’ and the order in layer to ‘1’.
  4. Add Box Collider 2D — Make sure to tick ‘Is Trigger’ and adjust the box collider around the sprite.
  5. Don’t forget to drag this game object into the prefab powerup folder.

Let’s Add to Our Code

Similar to the other powerups, let’s make some additions to our existing code. Let’s start in the ‘Player’ script and add:

  1. Trigger switch for our mega laser being active/not active.
  2. An offset for the mega lasers PosY so the beam comes from the front of the ship.
  3. A Serialized GameObject for our mega laser prefab, which we can drag and drop into in the Inspector.

Next, let’s create a method that calls our coroutine MegaLaserActive() and of course let’s create our coroutine that will handle the timer MegaLaserPowerupRoutine():

Now, let’s head into our FireLaser() method and add an else if statement to the existing code that is going to be checking whether the _isMegaLaserEnabled is true and if so, then we are going to next check that there isn’t already an instance of the mega laser game object in the scene and if there isn’t then we can create one that will last for 3 seconds. Further to this, we are going to set the transform of the new GameObject _megaLas to the parent (Player) so that the laser is attached to the player and will move around with the player.

Within our ‘Powerups’ script, let’s add the next powerup to the existing methods OnTriggerEnter2D() switch statement:

We now have our mega laser up and running and destroying enemies it touches within the 3 seconds it is available for. But, we have a minor issue which is, it is not destroying the enemies lasers as they are shooting them at us….how many mega lasers do you know that wouldn’t be able to destroy those tiny lasers? Right, so let’s quickly fix this in our ‘Laser’ script. Within the OnTriggerEnter2D() method, let’s add an additional if statement that checks to see whether the other tag is ‘MegaLaser’:

--

--