Adding Common and Rare Powerup Spawn Waves to 2D Galaxy Shooter in Unity!
Objective: To add 2 coroutines that handle the common and rare spawning powerups.
It is time to now bring some balanced spawning to our existing common and rare powerups and we are going to complete this by simply using our existing SpawnPowerupsRoutine() coroutine from within the “SpawnManager” script. There is no changes to where this coroutine is getting called (from within the SpawningEnemiesRoutine() coroutine in the “SpawnManager” script):
Creating some Variables
Let’s create some serialized variables of type GameObject so we can drag and drop the powerup prefabs into the Unity Inspector. We will create an array of 4 for the common powerups and 3 for the rare as shown below:
Now let’s dive into the coroutine that is going to handle the powerups spawning:
Here we will initially pause for 3 seconds at the start of the wave before the first powerup drops.
Then we will jump into a while loop that will continue to run until the player dies. Firstly, we are going to get a random number between 3 and 7 which is the random timer for when the next powerup will spawn, then we are also going to create a random Vector3 location for where the powerup will spawn and finally a random number between 0 and 100 which is going to be our % chance to spawn a common or rare powerup.
Now we are going to complete some If, Else-If checks that are going to see if the random number is between 0 and 30 (~30%) and then spawn a rare random powerup. Else-if the number is between 31 and 100 (~70%) then spawn a random common powerup.
Once it has spawned the powerup we will then wait for the _powerupTimer before running through a while loop again.