My 90 day journey becoming a Unity game developer: Day-18
Objective: Create a Speed Powerup. Make the player move at double the speed when they grab the speed powerup.
Let’s drag our Speed Powerup sprite into the Hierarchy. Scale down the sprite to 0.5 along each axis. On the Sprite Renderer, change the Sorting Layer to Foreground. Add a Rigidbody2D to the powerup, and change the Gravity Scale to zero so it won’t fall down immediately. Add a Box Collider2D, turn on Is Trigger, and scale down the box collider itself around the powerup sprite by clicking on the Edit Collider button. Last, change the name of the powerup to Speed_Powerup.
Question: Do I need to create another script for the Speed Powerup if it behaves almost identically to the other powerup?
Absolutely not. In Object Oriented Programming, it’s all about Modularity. Using one script to define the behavior all the powerups is more efficient than creating a new script for each powerup. I can control which powerup is assigned to each instance.
I’m going to attach the PowerUp script to the Speed Powerup, and turn the powerup into a prefab.
As we see, the Speed Powerup is using the Triple Shot from the PowerUp script. That’s because the triple shot is the only logic created inside the script, therefore we must create logic for speed to modularize our script.
Inside the PowerUp script, create a variable that will give unique id’s to each powerup name powerupID. Go over to the Inspector, drag the powerups into the Hierarchy, go down to the script component and assign the following values:
Triple Shot = 0 / Speed = 1
Instead of using the if/else condition, we will be using the Switch condition. We will be adding more powerups in the future, and having if/else conditions for like 4 or more powerups can make the code look messy.
Now we will implement the Speed Boost Powerup. To start, go to the Player script and create a variable that will double the amount of speed. We don’t want to change the speed variable values, which can give us problems in the game. Next, we need to know has the Speed Powerup been enabled. If so, the new variable isSpeedBoostActive which will be set to default of False will be changed to True.
I’m going to the PlayerMovement() method to set up the player’s movement using the powerups as well. Now our instinct is to use the transform.Translate and speed variable that we used to move our player to move the player after they grab the powerup. We use the If condition to check if isSpeedBoostActive is true or false. If it’s true, we will use the multiplier with the speed variable. If it’s false, we use the standard movement created. This code will work, but the speed variable value will not change when the player grabs the powerup. We want the speed variable to reflect the powerup’s doubling the value.
Instead, we will keep the movement code as is, create 2 new methods for the powerup activation and cooldown.
First method will be SpeedBoostActive(), which will set isSpeedBoostActive to true when the powerup is collected. Also, we will multiply the speed variable with the speed multiplier which will reflect the result in the speed’s variable slot. Last, we will start the coroutine SpeedBoostPowerDownRoutine().
Second method will be a coroutine name SpeedBoostPowerDownRoutine(). Inside we will give the player 5 seconds to use the powerup’s new speed value. After that we will return the player back to normal speed, and turn off the isSpeedBoostActive.
Next time we will be spawning the Speed Boost Powerup.