Objective: Implement a collectible secondary weapon system to your Unity 2D Space Shooter style game project which allows the player to fire lasers laterally for a limited amount of time.
Let’s set the scenario:…
So far, our little spaceship is equipped with a standard dual laser cannon and has a collectible triple-shot that fires a spread forward. Unfortunately, his flanks are left wide open to potential attacks. Now in his world, there are “Lateral Laser Cannons” available in his arsenal, but they were not fitted to his ship before he departed on his mission to defend the Galaxy.
The Space Battle Watch planning staff have captured this oversight and analyzed the player’s weakness. In order to assist, they’ve managed to beam ahead of the fight some Lateral Laser Cannon (LLC) units with self-contained limited energy core, with the hope they will eventually drift within his reach. These will offer our player the ability to fire laser shots from his left and right flanks for a limited amount of time.
Step 1 ~ Creating the Lateral Laser “shot” and the Cannons.
I simply took the original laser png file, duplicated it, modified its shape and color, and renamed it “Lateral Laser”. I left the Tag as LaserPlayer to ensure collision detection with the Enemy ships.
Added the Box Collider 2D, Rigidbody 2D, the Laser script to give it a behavior, and saved it as a Prefab.
I now need to create some cannons. After a bit of hunting on the web, I found this free asset and modified it as needed to make it suit my needs.
To create the Lateral Laser Cannon (LLC) Prefab, I started with an empty Game Object, renamed it “LateralLaserPowerUp”, attached my PowerUps script setting its default speed, ID, and associated AudioClip as needed, and finally attached a Circle collider 2D and a Rigidbody 2D.
Within this Game Object, I added two children; LateralLaserCanonRight and LateralLaserCanonLeft. I then attached my RotatingObject script (see below) to each and set the values so they would rotate in opposite directions, and flipped the “left” sprite along its x-axis.
When the LLC Prefab is set in the Game Scene, you have these two cannons floating together and spinning in opposite directions as they “drift” down the screen.
I added the left and right LLCs to the Player Game Object, turning them off when the Player is not equipped with them. When the Player collects the LLC Power-Up, I switch the SetActive back to True to make them once again visible.
Now that we have the Game Objects in place, let's get the code up to speed in order to implement the desired behaviors.
Step 2~ Implementing the required logic.
We start by adding a couple of new variables to capture the new Game Objects and the required bool to verify whether or not the Player is equipped with the LLCs.
I then added an IF statement to the PlayerFireLaser() method. This checks the boolean to see if the Player has collected the LLCs, and if so, rotates the left and right ones as required so they fire laterally once instantiated. The slight offset along the x-axis is to ensure the Lateral Laser shot isn’t instantiated from the center of the ship, but rather at the ends of the respective cannons.
In my version of the game, if my Player has received too much damage, he will lose a life. In the event he loses that life before time runs out on the LLC collectible, I didn’t want him to respawn with them still attached. These few lines ensure the boolean is switched back to False and that the Game Objects (cannons) both get turned off.
When the Player collects the LLC Power-Up, it flips the boolean to True, turns on both LLC Game Objects by turning SetActive to True, then starts the LateralShotPowerDownTimer coroutine.
The LateralShotPowerDownTimer coroutine allows the player to use his LLCs for up to 15 seconds (depending on the remaining ammo). After 15 seconds, the boolean _isPlayerLateralLaserActive turns False, and the LLC Game Objects are turned off (SetActive(false)).
We then need to adjust our SpawnManager script in order to expand the range of the randomPowerUp integer by setting the Random.Range between (0, 7).
Our final tweak is to go back into the PowerUps script, amend our comment to include “// 6 = Lateral Laser Canon”, and add case 6 to our switch statement to call the LateralLaserShotActive method in our PlayerScript script.
The final result is demonstrated here:
I hope you enjoyed this quick article. Hopefully, it will help you with your own Unity 2D Game Project. Thanks for reading :)