My 90 day journey becoming a Unity game developer: Day-17

Rhett Haynes
3 min readJun 13, 2021

--

Objective: Spawn the Triple shot powerup for the player to grab. The powerup will spawn for 3 to 7 seconds. When the player grabs the powerup, they will be able shoot the triple shot for 5 seconds.

First, we need to go to our SpawnManager script to create a coroutine for the powerup. Instead of tampering with the spawn coroutine for the enemies, we will rename the coroutine specifically for the enemy and create a coroutine specifically for the powerup.

Renaming the coroutine for the enemy.
Creating a coroutine for the powerup.

Second, inside the powerup coroutine we will use a while loop to check if we’re spawning. If so, we will randomly select along the X-axis from one side of the screen to the other, and on the Y-axis at the top off-screen. We will not use a container like we did with our enemies because when the player misses the powerup , the powerup will be destroyed immediately and you will have to wait for 3 to 7 seconds to instantiate again. Compared with the enemy who will be respawning immediately if it makes it to the bottom of the screen, that enemy will be reused and spawned from the container to allow us to have control over it.

Spawn powerup along the X-axis and Y-axis randomly.

Third, we need to Instantiate the Triple Shot Prefab. Create a new game object variable name tripleShotPowerupPrefab, and serialize the variable. Go to IEnumerator PowerupSpawnRoutine(), and instantiate the triple shot prefab using the Instantiate function. Use the triple shot prefab, posToSpawn variable, and no rotation needed. After, we need to pause the spawning to around 3 to 7 seconds randomly.

PowerupSpawnRoutine completed to randomize spawning powerups from 3 to 7 seconds.

Fourth, start the coroutine in the Start() area, using the StartCoroutine() function and passing the PowerupSpawnRoutine() as the parameter.

Coroutine assigned inside the Start() function.

Last, inside the Unity engine go to the Spawn Manager in the Hierarchy, make sure to click on Triple Shot Powerup Prefab and click on Overrides to make the appropriate changes to the prefab. Delete the Triple Shot Prefab and Enemy from the Hierarchy, and drag the Triple Shot Powerup Prefab over to the Triple Shot variable slot.

Updating changes made to the Triple Shot Prefab by selecting Overrides and applying all.
Dragging the Triple Shot Prefab to the Spawn Manager’s triple shot variable slot.
Powerup spawning every 3 to 7 seconds.

We will work on a new powerup call the Speed Powerup that will give the player a boost in speed next time.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet