Lesson 4.3 - PowerUp and CountDown
1 Hour
For Educators
The enemy chases the player around the island, but the player needs a better way to defend themselves... especially if we add more enemies. In this lesson, we’re going to create a powerup that gives the player a temporary strength boost, shoving away enemies that come into contact! The powerup will spawn in a random position on the island, and highlight the player with an indicator when it is picked up. The powerup indicator and the powerup itself will be represented by stylish game assets of your choice.
Project Outcome:
A powerup will spawn in a random position on the map, eagerly awaiting the player. Once the player collides with this powerup, the powerup will disappear and the player will be highlighted by an indicator. The powerup will last for 5 seconds after pickup, granting the player super strength that blasts away enemies!
Topics we'll cover
Recommended Unity Versions
2018.4 and Above
Lesson 4.3 - PowerUp and CountDown

Choose and prepare a powerup
In order to add a completely new gameplay mechanic to this project, we will introduce a new powerup object that will give the player temporary superpowers.

  1. From the Library, drag a Powerup object into the scene, rename it “Powerup” and edit its scale & position
  2. Add a Box Collider to the powerup, click Edit Collider to make sure it fits, then check the “Is Trigger” checkbox
  3. Create a new “Poweruptag and apply it to the powerup
  4. Drag the Powerup into the Prefabs folder to create a new “Original Prefab”

Destroy powerup on collision
As a first step to getting the powerup working, we’ll make it disappear when the player hits it and set up a new boolean variable to track that the player got it.

  1. In PlayerController.cs, add a new OnTriggerEnter() method
  2. Add an if-statement that destroys other.CompareTag("Powerup") powerup on collision
  3. Create a new public bool hasPowerup; and set hasPowerup = true; when you collide with the Powerup

Test for collision with a powerup
The powerup will only come into play in a very particular circumstance: when the player has a powerup AND they collide with an enemy - so we’ll first test for that very specific condition.

  1. Create a new “Enemy” tag and apply it to the Enemy Prefab
  2. In PlayerController.cs, add the OnCollisionEnter() function
  3. Create the if-statement with the double-condition
  4. Create a Debug.Log to make sure it’s working

Apply extra knockback with powerup
With the condition for the powerup set up perfectly, we are now ready to program the actual powerup ability: when the player collides with an enemy with the powerup, the enemy should go flying!

  1. In OnCollisionEnter() declare a new local variable to get the Enemy’s Rigidbody component
  2. Declare a new variable to get the direction away from the player
  3. Add an impulse force to the enemy
  4. Replace the hard-coded value with a new powerUpStrength variable

Create Countdown Routine for powerup
It wouldn’t be fair to the enemies if the powerup lasted forever - so we’ll program a countdown timer that starts when the player collects the powerup, removing the powerup ability when the timer is finished.

  1. Add a new IEnumerator PowerupCountdownRoutine () {}
  2. Inside the PowerupCountdownRoutine, wait 7 seconds, then disable the powerup
  3. When player collides with powerup, start the coroutine

Add a powerup indicator
To make this game a lot more playable, it should be clear when the player does or does not have the powerup, so we’ll program a visual indicator to display this to the user.

  1. From the Library, drag a Powerup object into the scene, rename it “Powerup Indicator”, and edit its scale
  2. Uncheck the “Active” checkbox in the inspector
  3. In PlayerController.cs, declare a new public GameObject powerupIndicator variable, then assign the Powerup Indicator variable in the inspector
  4. When the player collides with the powerup, set the indicator object to Active, then set to Inactive when the powerup expires
  5. In Update(), set the Indicator position to the player’s position + an offset value

Lesson Recap

New Functionality:
  • When the player collects a powerup, a visual indicator appears
  • When the player collides with an enemy while they have the powerup, the enemy goes flying
  • After a certain amount of time, the powerup ability and indicator disappear
New Concepts and Skills:
  • Debug concatenation
  • Local component variables
  • IEnumerators and WaitForSeconds()
  • Coroutines
  • SetActive(true/false)
Next Lesson:
  • We’ll start generating waves of enemies for our player to fend off!