More power-ups
In total there will be 3 types of power-ups in the following Space Shooter. The first power-up has already been added in my previous article. I will now add the second power-up, which is a speed boost together with improvements to make it easier to add further power-ups in the future.
Drag the first sprite onto the Hierarchy and add animation the same as I am showing in this animation article:
As all power-ups will need a Rigidbody and a Collider, we will add two RequireComponent to the Powerup script. When you add the script to the game object now, the two components will get added automatically:
Adjust the size, set Sorting Layer to foreground, set Gravity Scale to 0 and enable Is Trigger. Now turn the new power-up into a prefab by dragging it into the Project window:
As we will no longer have one power-up only, we need to swap out the single GameObject for an array in the SpawnManager script. We will also need to update the instantiation with a random selection of the prefab in the array:
Set the array to 2 in the Inspector and add the prefabs:
In the Powerup script add a public enum on the bottom, outside of the class for power-up type that we will use to identify the power-ups. After that add the enum to the class and serialize:
Ensure the prefabs have the PowerupType set correctly in the Inspector:
Open the Player script and add the variables needed for the new power-up. This is similar to what we did in the first Power-up article:
Also add the coroutine for the speed boost:
Update player movement to check if speed boost is enabled and if so, move at faster speed:
Update the EnablePowerup function to take in a PowerupType. Also add if statements to check which PowerupType was in the function call:
Add powerup type to the function call in the Powerup script:
Lastly, set the speed boost amount in the Inspector:
Good luck!