Problem: Your player is strafing back and forth unleashing a continuous stream of laser fire and destroying enemies before they even make it onto the screen.
Solution: Limit their ability to fire continuously.
We have already added a limit on how quickly they can fire by setting up a fire rate for the player.
Now we can try and limit their amount of ammo. By making the ammo a limited resource, the player is less likely to spam the fire button and use it only when it is sure to hit something.
There are a few steps involved in setting this up. Ammo Count, Reloading, Display and Shooting ‘Blanks’.
Setting up an Ammo Count is as easy as adding a few variables to your Player class. I have added Ammo Count, Ammo Clip and Max Ammo to this game. The Ammo Count will frequently change. The Max Ammo not so much, I leave it exposed because it may make for a good power up/ level up feature to increase the Max count later. Ammo Clip will be how much is loaded each time the proper power ups are collected.
For the Reloading, I set up a Reload method that can be called from the Ammo Power Up as well as the Triple Shot Power (because what use is firing a burst if you have no ammo, right?).
The Reload() also sends out an Event that is picked up by the UIManager. The UIManager uses the Event Parameters to update the Ammo Display. You can learn more about updating a Text display in my Let Me Tell You About article.
As for the Shooting ‘Blanks’ step, it takes a bit more modification to the Weapon() to pull off well.
As you can see, We have added one more condition to the IF statement to make sure the Player has Ammo to fire. We remove a round when a shot is fired. And we added a Else If to the end to allow us to cause a visual effect when trying to fire without Ammo. In this case, we are using a spark particle effect in front of the Player Ship. It is setup very much like Normal or TripleShot in that it has a Prefab and an Offset assigned to it.
The Final step is setting up a Power Up for the Ammo. This follows the same principles used in Power Ups and Switch Statements.