Professional Unity Developer Program | Day Twenty

Space Shooter Game | Triple Shot Powerup Ability

Jordan T Kay
5 min readSep 20, 2022

Objective:

Create Triple Shot Prefab | Give player the triple shot ability once icon is collected

Create Triple Shot Prefab

Triple Shot Prefab

Once our player collects the Triple Shot Ability, it will now shoot these lasers.

Give Player the Triple Shot Ability Once Icon is Collected

What are we asking of our game?

Once our player collects the triple shot icon, give it the ability to shoot three lasers.

What do we need to do to make this happen? We need to…

  • Edit our player script to have triple shot active or not
  • Edit our powerup script to turn on/off the ability in our player script

To do what we need our game to do, we need script communication. Let’s start by editing our player script.

Player Script

What are we asking of our script?

If triple shot is active, then we want to give the player the ability to shoot 3 lasers.

We need to establish an if/then statement, but where within our player script should we do this?

We need to edit our FireLaser( ) method as it controls our player’s firing functions. Within the method lets add code that states if tripleshot is active, then shoot three lasers. Else, shoot one laser as normal.

To do this, we need a bool variable to hold whether or not triple shot is active or not.

We also need to create a variable to hold our tripleshot prefab that our player will shoot.

Next, let edit our FireLaser( ) to add our if/then/else statement.

As our script stands, the only time we shoot triple shots is if its enabled. What makes it enabled?

We need to create a public method that turns our bool statement from false to true. We also don’t want the ability to last forever, we need a time limit on the awesomeness that is the triple shot.

What are we asking of our script?

Enable/disable to triple shot feature. If the triple shot is enabled, disable it within 5 seconds.

This means we need a public method to call upon to set the bool value of triple shot to active and also a coroutine to turn the bool value to false after five seconds.

The next question we need to ask ourselves is, what needs to happen for triple shot to be activated?

We need to collect the triple shot powerup icon, which means we need to edit the Powerup Script.

Powerup Script

We already know we need to make a modular system as we have more than one powerup we want to implement into our game. This means, each powerup icon is going to be attached to this script, so we need to create a variable to hold our PowerupIDs. How do we decide which powerup gets which id? Lets look at our Array within our spawn manager.

Based on this, we will use the same int values that the powerup icons are assigned to.

  • Triple Shot Powerup = 0
  • Speed Powerup = 1
  • Shield Powerup = 2

Now that we have our powerupID situated, lets create a modular system for all of our powerups. For the Speed and Shield powerup, lets just use a Debug.Log that tells our game we collected the icon until we can replace it with a Method that will gain the ability to our player.

Using an on trigger event, lets add the following code in our Powerups Script.

Decoding the Code (line by line)

  1. Declares an OnTriggerEvent
  2. States that if our powerup icons collide with a tag name “Player” do the following code
  3. Creates a variable called player to gain access into the Player script
  4. If the program grabs the Player script it will run the following code
  5. if PowerupID is 0, then run the method TripleShotActive( ) in our player script
  6. if PowerupID is 1, then print in the console “Collected Speed Boost”
  7. if PowerupID is 2, then print in the console “Collected Shield”

Now that we have created this script, we need to assign the powerIDs on our powerup Icons.

Triple Shot Powerup Prefab
Speed Powerup Prefab
Shield Powerup Prefab

Let’s see if we gain the ability of a triple shot once we collect the icon.

--

--

Jordan T Kay

Join my journey into game development with Unity. Learning all I can to produce quality games. 🚀