Framework 1: Secondary Fire (part 1)
Tired of just shooting your own tiny little lasers? Sad that your puny laser keeps missing the target? Frustrated that you can’t play your own creation because you suck at this game? Then it is time to create a secondary weapon that can delete everything in front of our player! This secondary fire will work independently from the main fire, and will have a limited ammo of 3.
How it works:
Since this is an independent firing systems, I will be mapping the firing key to the keycode “E”. So when “E” is pressed it will instantiate a giant laser graphic with a big hit box that would basically delete everything in front of the ship. The laser would last about 5 seconds then turn off again.
So what we will be needing this time is The Player script, UI Manager script, Enemy script and some laser graphics.
So for the graphics, I basically drew a box in Adobe Illustrator and added a 3 tone gradient with the outside tone being transparent. In Unity I made the image Draw mode “Sliced” an gave the laser a Box collider 2D and Rigidbody2D. As for the animation, I just widen the Width from Zero to 1.9 and back to zero after five seconds. I also create 4 individual images that will be part of an array similar to that of our lives system. These will be part of the UI to tell the players how many shots are left.
The Player Script
In the Player script we will be creating a new Game Object handle for the Big laser and another new Integer handle for the total amount of shots available before you are empty. Then you are going to create a timer and fire rate float to determine how fast you can fire the second time. (don’t want to rapid fire this big laser now do we?)
In the start method, you are going to disable the Big laser so that it doesn’t just show up when it wants to.
Now we are going to create 2 new methods and 1 new coroutine. The first new method controls the amount of times you can fire and makes sure the player will never have more than 3 shots at any given time.
The second new method is going to control the new coroutine and also handle the subtracting of 1 from the total 3 shots and also containing the fire rate and can fire timer.
The Coroutine will be focus on controlling when to set active the big laser, and after a certain duration disable the big laser.
Finally on the Update method, we are going to check when the “E” is pressed fire the Big Laser, and when laser capacity is at zero, stop firing. We do this with a basic if statement and Input command.
The UI Manager Script
Now it is time to update the UI manager. Create a new Image handle and also an Sprite array to contain the different stages of laser capacity.
Then create a new public method called with and integer value and have the sprite array equal to the image component + the integer value.
The Enemy Script
To make sure the big laser does damage the enemy, we need to make sure the enemy is aware of the big lasers hit box.
Similar to how create the basic if statements for enemies tracking laser or player tags, we are going to do the same for the big laser. I created a new tag called “Beam” and added a new if statement to check for any collisions that are in contact with the Beam tag.
Finally back in the Unity Inspector, drag and drop the Laser prefab into the player slot so it can be instantiated! Save and time to try out our new weapon of mass destruction!