Phase 1: Secondary Fire Powerup -Part2

This is part 2 of a new weapon powerup that I’ve been working on that’s part of the Phase 1 set of challenges from the GameDevHQ Course. That article can be found here.

UI Systems

I decided to start implementing this weapon by creating the UI system first. Since we’ll be using a charge/energy system I decided to give the player 10 energy points and that the powerup would need 10 energy to fire the weapon.

Once I created the UI elements it was time to update the bar by creating a new public function in our UIManager. First I need to get a reference to the images I’ll be using to swap out to indicate the level and then to get an array of the objects that are used to display the level.

After I’ve done that I need to write the function to update the UI based on a number we’ll provide through an int. This function works exactly like the other systems we’ve implemented with the lives, ammo and shield system.

Player Systems

Next, I’ll create the powerup system and then later the actual firing logic. At this point we haven’t even touched the actual weapon we’ll be building but I thought to leave the best (and hardest) for last.

I added a new private int variable and named it “_energy”. This is important now to create the rest of the logic/systems. I proceeded to create a new case in our powerup switch with the ID of 5. To avoid out of bounds or null errors I first run a check to see if our energy is lower than 10 before we add energy to our variable as well as updating the UI. While I added the new powerup I got the idea to use the excess health powerups to convert them into energy as well. Therefore if our player has 4 lives and collects a powerup that health will be used as an energy point instead.

The time came to create the input and firing system for the powerup. I have a function “Fire()” and in there we have the logic to shoot our laser. It made sense for me to create the logic in this function with the existing logic. I first took a look at my Input Manager since I’ve been using that throughout my game to create the input logic once in code and if I need to make changes to my Input I can do so later without revisiting all my code.

As you can see we have an input called Fire2 and I’ll be using that as my button to fire the secondary weapon. Back in my script, I created an if statement with “Input.GetbuttonDown(“Fire2”)”. Inside the if statement I ran another if statement to check that I have the required 10 energy and that my lives are greater than 1 since 2, 3 or 4 would be greater than 1. If I meet the requirements It would set the energy to 0 as well as reducing one life from the player, update the required UI elements, and instantiate the weapon. That reminded me to create a Unity variable I can reference in my instantiation. I created a serialized private GameObject and called it “_orbOfDestruction” in my variable section. However, if I did not meet the requirements to fire the weapon I wanted to play an error sound clip.

Since we already have reference to an audio source I decided to use that and add two more sounds to play when I fire off the secondary weapon as well as when I don’t have the required cost.

Everything in the player script is done. I still need to create the powerup that would give the energy when collected and lastly create the actual secondary weapon.

Creating the Powerup

Before we can create the actual powerup in our game we need to create the logic in our powerups script to tell the player which powerup to activate.

In the Powerup Script, there is a switch function and I’ve created a new case with an ID of 5 that will activate the corresponding powerup when we assign the ID to the powerup in the Inspector.

With all of the logic done, it was time to create the powerup and add it to the list on the SpawnManager.

I’ve taken an existing powerup from the prefabs folder and added it to the hierarchy. Next, I unpacked it to create a new powerup without it overwriting existing powerups. I set the powerup ID to 5 and replaced the image with a fitting image, and dragged it to the prefabs folder from where I dragged it to the array of powerups on the SpawnManager.

I decided to test everything I’ve been working on up until this point. I couldn’t really test the shooting mechanics since I don’t have an object to “shoot” and would cause null errors. The next best was to test the health and energy powerups to make sure that worked.

All that’s left to do is create the actual secondary weapon I’ll cover in the 3rd and final article here.

Self-taught Unity and C# Developer with a passion for games and the stories they tell.

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