Balancing Powerup Spawns
Now that we have added in a couple new types of powerups, let’s look at how we can balance out the rates of the spawns so that we have a much higher chance to get ammo, and a lower chance for health based powerups. If we kept the current rates, we are looking at a ~16% chance that we will spawn ammo, which can feel like forever in some cases when the player is out of shots.
There are multiple methods that can be used for this process, however in this case, we will use a switch statement within our spawn manager script. We will first create a new variable to work with:
With this value, we can go into our SpawnPowerup routine and give it a random range value that will be created whenever the routine starts up again:
We will add the debug.log for now to see the values that roll in our game when we play and make sure they properly match what should be spawning with that number. Next, we will start to make our switch statement to determine the powerup that will spawn:
What we can do with our switch statement in this case is forgo the range for each case and type it all in the initial statement. Then, whatever the value becomes, it knows that you want it to be the specific power up and it will call that case. As well, if it happens to be 64, it will call the lowest possible case, which in this case is Ammo. Health and Tsunami technically fit into that value, but switch cases will choose the first available option to pull from.
Now that we have all of this put in, we can take a look into how it looks within our game and see if the numbers match what we want it to be with our desired cases:
As we can see from above, the powerup we want to spawn is showing up if it’s value is rolled, and no extras come in cause they fit the criteria.