In this article, we will make it so that the Ammo Power-Up spawns frequently and the rest of the Power-Ups rarely.
In the SpawnManager script, we need to add some variables. First are two floats, one for the chance of Power-Ups in the uncommon array to spawn and the other for the rare array. Then we need a GameObject for the common Ammo Power-Up Prefab and two GameObject arrays for the uncommon and rare Power-Up Prefabs. The last variable we need is an int to keep track of how many rare Power-Ups have spawned in a Wave.
This method returns the selected Power-Up based on a random value between 0.0 and 1.0 or nothing if the Power-Ups shouldn’t be spawning. If the random value is greater than the uncommon chance, we return the common Power-Up Prefab. If the random value is less than or equal to the rare chance, we return a random rare Power-Up Prefab or the first if there is only one, and if the Wave has reached the maximum rare Power-Ups, we return the common Power-Up Prefab. For everything else, we return a random uncommon Power-Up Prefab or the first if there is only one.
Now to update the SpawnPowerUpRoutine to use the different groups of Prefabs. We need to call the SelectPowerUp method and store the returned GameObject for instantiation if it is not null. And set the number of rares spawned back to zero at the start of the Coroutine.
On the SpawnManager, you can see how the Power-Ups are sorted.
Each Wave, the Player gets a chance at a set number of rare Power-Ups, an opportunity for uncommon Power-Ups, and lots of Ammo.