Phase I: Secondary Fire Powerup
This article will go over how I set up a secondary fire powerup to add some variety to how the player fires lasers.
Objective: To create a collectible for a new type of projectile. Also, have it spawn rarely and last for 5 seconds.
I opted for a Multi-Shot type of powerup that will shoot multiple lasers in an arc in front of the player when fired.
The first things I did were create a method within the Player script to be called when the powerup is collected and a coroutine to power down the multi-shot after 5 seconds.
The above method first turns off Triple-Shot by changing _tripleShotActive to false just in case it is currently active. Then multiShotActive is set to true. I also call the AmmoCollected() method to give the player some ammo in case they are currently empty. The final action in the method is to start the MultiShotPowerDown() routine, which I’ll show below.
This coroutine waits for 5 seconds then changes multiShotActive to false.
Next, I created a MultiShot() method which controls how the laser will behave when Multi-Shot is activated.
I played around with the angles and the degrees of change and left them hard-coded for now. I may change that later to use variables to make adjustments easier. The code fires a laser every 15 degrees across the arc until the fireangle exceeds 83.
Next, in the LaserControl() method I have in the player, I add an “else if” statement to check if multiShotActive is true. If so, I call the MultiShot() method.
I created a Multi-Shot sprite by editing the existing powerup sprites and used it to create a prefab for the Multi-Shot.
In the SpawnManager in the Inspector, I change the number of elements in the _powerups array to 6 and drag the Multi-Shot_PowerUp into the slot.
I also created a PowerUpSelector() method and gave the different powerups different chances to be spawned.
The PowerUpSelector() method is called from within the SpawnPowerup() method and determines which powerup actually gets instantiated by the SpawnManager.
That is my secondary fire powerup solution. I may tweak some things as time goes by, but I am pretty happy with my results so far.
In my next article, I’ll modify the Thruster that was set up on the player in a previous article here. Make sure to check it out when it’s published.
I hope you found this as interesting to read as I did to implement in the game. And until next time, I wish you well on your coding journey.