My 90 day journey becoming a Unity game developer: Day-14 & 15

Rhett Haynes
4 min readJun 11, 2021

--

Objective: Add a triple shot powerup that the player must grab. The powerup will be destroyed when it goes off screen. When the powerup is collected, Triple Shot will be activated and deactivated after 5 seconds. The triple shot lasers will be destroyed when they go off-screen to not accumulate in the Hierarchy and live in the game forever.

We will drag our powerup sprite into the Hierarchy. Scale down the powerup to a reasonable size compared to the player’s size. In the Sprite Renderer, change the Sorting Layer from Default to Foreground. Add either a box or circle collider, and check the Is Trigger box. This will allow the player to go through the powerup and make it look like the powerup was collected by the player. Add a rigidbody as well and change Gravity Scale to 0. Make the name of the powerup to Triple Shot Powerup. Drag the Triple shot powerup into the Prefab folder to make it a prefab.

Triple Shot powerup created.

Create a new script call PowerUp and drag it onto the Triple Shot Powerup game object. Create a new float variable name speed and assign its value to 3. In Update(), set up the powerup’s movement with the transform translate function. Then multiply it by speed and real-time.

Setting up the Powerup’s movement.

Now, we want the player to collide with the powerup to make it look as if it was collected. When the player collides with the powerup, we will destroy the powerup to make it disappear using the OnTriggerEnter() function. We will look to see if the player collided with the powerup by checking the player’s Tag. Also, if the powerup moves off-screen downward, we will destroy the powerup as well by using the following code inside the Update() function:

The Powerup is destroyed when the player collides with it and when it goes off-screen.
Triple shot powerup destroyed by player grabbing it and going off screen.

Now we’re going to activate our Triple Shot when we grab the powerup. Go to the Player script and let’s activate the triple shot. We need to change our isTripleShotActive variable from false to true, but we don’t want to change the variable directly which can cause problems in the game. Instead, we will create a method call TripleShotActive(). There we will set the variable isTripleShotActive to True. Create a coroutine using IEnumerator name TripleShotPowerDownRoutine() to allow the player to use the triple shot for 5 seconds and deactivate it. Then we will go back to the TripleShotActive() method and start the coroutine TripleShotPowerDownRoutine() after isTripleShotActive is true.

Head back over to the Powerup script and go to the OnTriggerEnter() function. If the parameter other is the player being passed, we want to communicate with the player’s script to access the TripleShotActive() method when the player collides with the powerup. To do this, we will create a variable name player which will get the component Player script. For error checking purposes we will check to see if the player exists. If so, use the player variable to access the TripleShotActive() method.

Triple Shot active, but the lasers are still active in the game forever.

Last, we need to destroy the Triple Shot lasers so they don’t accumulate in the game. Inside the Laser script and inside Update(), under the if condition checking to see if the lasers go above 8 units, we need to check if the triple shot lasers have a parent laser in this situation. If the parent is not NULL, we want to destroy the game object of the parent.

Triple shot lasers destroyed when they go off-screen.

Next time we will work on animating the Triple Shot Powerup.

--

--