Phase 1: Secondary Fire Powerup -Part 3
This is the final part of our elaborate secondary weapon I’ve been working on. It’s time to go big or go home!
So far all of my previous systems are in place, tested and awaiting their new weapon the “Orb of Destruction” (spoiler: it ended up looking more like an EMP cannon).
The secondary weapon will consist of two parts and each part will have a respective script for its behaviour. The first object is the “base” of the weapon which will spawn at the centre of the screen and will fire off 5 blasts with a cooldown of approx 1.5 seconds. Once the cycle was completed successfully it will destroy itself and give the player back the one life it took. But the orb will be susceptible to damage from enemies when there is no blast so if an enemy manages to destroy it then it will not return the life it took.
The second part of the weapon will be the actual blasts. When a blast is created it needs to expand smoothly over a set of time and as it expands it needs to destroy enemies. This means we’ll be making minor changes to the enemy scripts to add another method of damage.
I have two images that are actually supposed to represent a mine, but since I’m not an artist I decided to use those as the base of the weapon. I dragged the first Image I wanted to use to the Hierarchy and as a child added the second image. That would act as a rotor that would rotate periodically to add some animation to the attack. I’ve also added an audio source to the weapon so we can add some sounds when the blasts are created.
Next, I created the first script and called it “SecondaryWeapon” and attached it to the secondary weapon.
On the script, I needed to define quite a few variables. I had to define how many times the orb would release a blast and how often a blast should occur. I also needed to get a reference to the RotorBase to rotate it, I needed a bool to check if the orb gets destroyed, then I also needed a reference to the actual blast that will be instantiated. I’ll also need a reference to the audio source and the clip(s) I’ll be playing, and I’ll also need a reference to the explosion prefab in case our orb gets destroyed. There is too much happening between the start event, coroutines and OnTrigger events to explain everything in detail so I’ll just be sharing the script to show you what is happening
As you can see there is quite a lot happening on that script. When the orb is spawned in then it will start that Coroutine that would release a new blast or wave between every cooldown for a maximum of 5 charges. Once the 5 charges are depleted it ends the routine and gives the player its life back. The OnTriggerEnter2D event is to check if an enemy is shooting on the orb or if an enemy is flying into it to destroy it.
Have you ever heard of a Lerp? If not it’s short for Linear Interpolation and it’s a mathematical function in unity that returns a value between two points at a point on a linear scale. It’s really useful if you want to animate buttons and other objects in unity. I’ll be using this to gradually enlarge an image over a short time span and as the image is growing bigger and get in contact with enemies will kill the enemies. When the image reaches the second value, we’ll destroy the wave blast.
After writing the script it was time to create the blast object in the hierarchy and attach the script and set everything up.
I dragged the image I’ll be using to the hierarchy and attached the script to the object. Apart from that, there weren’t much to do but prefab the blast and orb objects and assign them to each other. On the Player, I needed to add the Orb of Destruction, and on the Orb I need to add the explosion wave. I also need to add the sound clip I’d like to play with each wave that is created.
Once I double-checked that everything was set up correctly and that the prefabs were saved correctly it was time to test the new powerup. This powerup took quite a while to obtain since you needed 10 energy to spawn the orb. I’ve written in a secret “cheat” where I can press a button that would fill up my charge bar and if I’m low on lives then I’d get an extra life as well. I might cover the cheat in another article because I had to put that code in a special statement that would prevent the code from being added to any of my builds.