My journey becoming a Unity game developer: Determining how long powerup effects should last. Making the Triple Shot Powerup spawn at random times.

Objective: Make the Triple Shot Powerup spawn at random times and random positions for the player to collect. When collected the player will have a certain amount of time to use the Triple Shot before going back to shooting regular lasers, and needing to collect another powerup.

Triple Shot PowerUp spawning every 3 to 7 seconds from the top of the screen.

We want to have powerups spawn off the top of the screen. However, we want them to spawn from random locations at different moments in time. The locations is the easy part as they will spawn by the width of the screen using the X-axis. The time for how long we should wait in between for the next powerups to spawn is something we might have to play around with. The time we select can effect the difficulty of the game for the player as they fight more difficult enemies, and will need powerups to help them get out of sticky situations.

To begin, inside the SpawnManager script, let’s create a new variable with the SerializeField attribute. Its Access Modifier is Private with a Type GameObject name _powerupPrefab. This variable will give us access to the Triple Shot PowerUp prefab that will need to be instantiated when it’s ready to spawn.

Next, create a new coroutine using IEnumerator name SpawnPowerUpRoutine(). This coroutine will need to spawn the powerup at random positions, and at random times. Let’s start by checking that While _stopSpawning equals False. When this condition is False, copy the randomX variable from SpawnRoutine() to set the Random Range along the X-axis.

Then, create another new variable to set the spawn position with a Type Vector3 name spawnPos. Set its value to a new Vector3 using randomX on the X-axis, 8f for the top of the screen on the Y-axis, and zero on the Z-axis.

Create one more new variable which will be a Type GameObject name spawnPowerup. This variable will Instantiate the powerupPrefab, at the spawnPos transform position, with no rotation using Quaternion.identity.

Last thing we need to do is to pause between spawning using Yield. Then return a new WaitForSeconds of 5f seconds to start out with.

Created a Spawn Powerup coroutine to spawn the Triple Shot Powerup prefab every 3 to 7 seconds.

Inside the Start() function, we need to use StartCoroutine() method, and pass in the SpawnPowerUpRoutine() coroutine. This will start spawning the Triple Shot Powerups as soon as the game starts.

Start the SpawnPowerUpRoutine() coroutine inside the Start() function.

We also affected the Triple Shot effects for the player by using a TripleShotPowerDownRoutine() coroutine to wait 5 seconds before turning off the Triple Shot. This coroutine is used inside the TripleShotActive() method after the _isTripleShotActive is set to True when the player collects a Triple Shot PowerUp object.

Triple Shot lasts 5 seconds before cooldown happens using a coroutine.

Inside the Hierarchy, select the Spawn Manager game object, and drag the Triple Shot PowerUp prefab into the Powerup prefab slot located under the Spawn Manager script component. This will allow the script to access the powerup’s prefab through the powerupPrefab variable.

Triple Shot PowerUp prefab added to the Spawn Manager script component.

Play the game, and watch the Triple Shot PowerUp spawn every 3 to 7 seconds from random positions at the top of the screen.

Triple Shot PowerUp spawning every 3 to 7 seconds from the top of the screen.

One final thing we did quickly was organize the prefabs inside the Prefabs folder. Create a new folder name Enemies, and drag the Enemy prefab into it, as this folder will now hold all enemies created for this game. Create another folder name PowerUps, and drag the TripleShotPowerUp prefab into it as this folder will hold all powerups used in this game.

Created Enemies and PowerUps folders to organize their prefabs.



Learning to become a Unity game developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store