Day 40: Secondary Power UP! Part 2!
Objective: Create the power up to enable the missile!
Yesterday was setting up the missile to move forward until an enemy was found and than rotate and go after it and destroy it! So today we will set up the power up with a weighted system to spawn rarely.
We’ll start on the player script setting up the logic to start firing the missile. We need a public function that set’s a bool to true and a coroutine to turn it back to false.
Under the firing function we’ll check to see if the missile is active and if so instantiate the missile.
Now to the power up script to call the missile function. Set up a new case and call it.
We need to set up the game object to be collected. We’ll copy every other power except we’ll add the correct ID. After that add it to the spawn manager.
It’s time to refactor our spawn manager to implement a weighted system. On the power up script we’ll add a new variable called
In the inspector add a weight to each of the power up. If you want it to spawn a lot more have a higher number and if you want it rare do a lower number. For our missile we are doing 30 and the rest are about 60–80.
To figure out the weight we need to go into our spawn manager and input the logic.
We’ll need a function to add all the weight up. To start off we need a variable to save all the weights in one variable. After creating the variable we will use a for loop to loop all the power ups to add the weights up. Now call this in the start function.
Next we’ll get a random number from 0 to the total weight. After the random number is assigned we’ll go though each of the power ups and get it’s weight. Once we have the weight we’ll see if it’s greater than the random number that was generated. If it is assign that powerup to a selectedPowerup. After that we’ll take a way that
Once the power has been selected we’ll pass it into the Coroutine to spawn. We will need to refactor a little bit to call the new function for the correct power up.
And we are finished! The way this weighted spawn system is set up, you will be able to add any number of power ups and just add the weight to each powerup and it’ll spawn.