2D Shooter: Balancing Spawn Rates
Objective: Play around with numbers to get a good balance between enemy and powerup spawning. Let’s fix a timing issue too.
So when we’re making a game, we want to make sure the balancing is set to match the level of difficulty we want it to be. A harder game will likely have less mechanics to help the player, and an easier game will have more.
At the moment, I feel like my game is a bit on the easy side with the rate of powerups spawning. I get extra life when I’m often already full, as well as ammo. The multi-shot powerup that is supposed to be rare also spawns a bit too often then what I would like.
So let’s change the spawning a bit.
I also reorganized the code so hopefully its more straightforward as I got rid of some redundant lines from before. I made a new array for rare powerups, like the multishot, and made it so they had separate randomizers. But one randomizer, probability, will determine if a rare or a normal powerup gets spawned. This way, about 1/10 times a rare one spawns, and the 9 times its a regular. Of course, these are just chances, so a multishot might still pop up two times in a row.
I also moved the health pickup to element zero, which is separate from the other powerups. That’s why randomPower has a range starting from one instead of zero.
I also want to increase the wait time at the end, so the player has to go a little bit longer before another powerup spawns.
As far as health and ammo goes, I got rid of ammo as an enemy drop and kept it as a normal pickup and something that spawns when the player runs out of ammo. I also extended the range of randomNum so health spawn less often.
Now one thing that made balancing difficult was that I had a bug that made it so that the wait time between enemy and powerup spawns were getting shorter as you progressed the waves. With help I figured out that it was because when a new wave started, some of my coroutines hadn’t finished playing through their code yet, and so time got desynced between waves.
To fix this, there is this handy command called StopAllCoroutines().
Once this was integrated, it fixed my issue. When I new wave starts any coroutines that were still playing gets forced to stop. When they’re called again, they will start at the beginning.