Creating a Powerup system (Game Dev day 19)
Objective: Create a speed boost powerup for the player character
Let’s break this down logically before we get into the coding necessary to make this happen. We need to be able to increase the movement speed value of the player character when a certain condition is met. In this example, we the player has collided with a power up. You could also do this with a key press (Holding shift makes you move faster).
So first thing is we need to set a parameter on the player for the boosted movement speed. This will be a variable in our Player.cs script, and we will name it _Boostedspeed and we will set it to a value of 8 (our base speed value was set to 5).
So now we have the variable set, so our script can understand the concept of a boosted movement speed. However, right now there is now method to actually achieve the movement boost. The next thing we need to do is create the powerup “Speed” itself.
Start off by creating an Empty Gameobject. I am going to name it Speed_Powerup for organizations sake.
Right now there is no sprite associated with this powerup, so lets give it a graphic (See my day 17 tutorial for an in depth animation guide).
Place your sprite frames into the Unity Animation window and record the animation. It should look like this when completed.
Great! now we have the physical object. However, we need to add a few things to it to make it play nice with our scripts. Seeing as we are going to need to “pickup” this powerup, it will need a Rigidbody and a Collider (Make sure both are 2D)
Now its time to code the logic for this system to work. We are going to make a powerup.cs script and attach it to our Speed_Powerup gameobject. Drag and drop your new script onto the Speed_Powerup Gameobject in the hierarchy.
Now open up that Powerup.cs script.
Now we want this powerup to fall down the screen towards the player, just like our enemies do. So let’s start off with that.
All we need is a transform.translate in void Update. Let’s also set a limit so that if the powerup goes below the player’s screen, it despawns.
That’s great, but right now nothing will cause this powerup to spawn. Just like our enemies, we need to set spawning parameters. The good news is we can use the same Spawn Manager as we used to set up our enemy spawns.
Let’s open up that C# script now.
We are going to need a coroutine for the powerup spawns, again just like our enemy spawn coroutine. First let’s set a variable for the powerups in the SpawnManager.cs. We will call it _powerups.
Let’s make that coroutine. First, take a look at the Enemy spawn coroutine we built previously.
Our powerup spawn coroutine is extremely similar, but we will change a few values and add a line of code to support multiple types of powerups.
The first line of code is 1:1, it handles the position the powerup will spawn at. It spawns just off screen, the same as the enemies.
The second line allows us to create multiple powerups to be handled by the same spawning coroutine. We have already made the triple shot, and we will be making a shield after we finish the speed boost (hence the range).
The third line instantiates whatever powerup was selected.
Finally, the fourth and final line sets this coroutine to loop after a delay between 3–8 seconds.
Make sure you add this coroutine to void Start in the script, otherwise it won’t do anything!
Let’s move back to our Powerup.cs script. We need to set what effect the player should receive when colliding with whatever powerup spawns.
Remember the range we put on the SpawnManager?
int randomPowerUp = Random.Range(0, 3);
This is where that logic is used.
Based on the value of that random number, we will get a different powerup. We use Getcomponent to interact with the player (because that’s where we are going to code the actual effects of the powerup) and then use a switch command to have multiple outcomes dependent on which powerup we collide with.
Let’s move to the final script we need to interact with, the Player.cs and let’s review the triple shot powerup’s logic.
We are going to do the same exact thing for the Speed boost powerup.
One last thing to do is drag your Speed_Powerup gameobject into the Powerups component of your Spawn_Manager.
And that’s it! We now have 2 working powerups, and are going to make a third next (a shield that will block a hit from an enemy).