Rapid Fire Power Up
In this article I am go going to go over how to add a Rapid Fire power to a Shoot’em Up (SHMUP) game.
The Player script is controlling all of the Player’s abilities and activation of Power Ups. We are going to declare to a bool that will control when the Rapid Fire is enabled and another that is a float that will decrease the fire rate (meaning the player will be able to fire faster).
I am sure there is a more optimized way of doing this and I will revisit the script later to clean things up, but I copied the FireLaser method and created a RapidFire method with it, except for one difference. The fire time is adjusted.
Let’s go over what fire time is and how it is controlling the speed of the Player’s firing. In order to fire, we are using IF conditional to see if the the Space key was press and if Time.time is greater than the fire time and if that is true, either FireLaser or RapidFire will trigger. Time.time is tracking time since the game began, so it starts at 0 and increase from there. At the beginning of the game we set fire time to -1 so it is already less than Time.time which is 0. Once the player fires, we set the fire rate to the current time of Time.time plus the fire rate so it will now have to wait that amount in order to fire again. For example, if the player fire at 1 second, the new fire time will be 1 + fire rate, which is .5. So, the player will wait until time (Time.time) reaches 1.5 before firing again. The rapid fire rate is a smaller value at .25, so the player has to wait 1/2 the time before firing again.
I am envisioning a Shooting Bar that will represent the heat of the gun and if it gets too hot, the gun will overheat and you will have to wait for it to cool down before fire it again. This Rapid Fire Power will act as a coolant that will allow the Player to fire rapidly for a set amount of time with adding any heat to the gun. This will be a future development.
Back to the script, Rapid Fire is enable when that bool is turned to TRUE and that happens when the Power Up is collected.
In setting the rapid fire bool to TRUE a method was created so it can be called from the Power Up script. I am not going to go over the Power Up script and Respawn Manager script as I have covered it inn previous articles. But, when the Power Up script calls the Rapid Fire Active method it will set that bool to TRUE and start a coroutine that has a Wait For Seconds of 5 seconds, this is the duration time the player can use the rapid fire, before the bool is set back to FALSE, which disables that ability until it is collected again.