Today's article is the second part of the triple shot powerup tutorial. For the first part, go here. We will not go over the animation that is in my examples in this article.

Open your project and create and drag the sprite image that you want as your powerup into the hierarchy and rename it ‘Triple_Shot_Powerup’. Let’s change the sorting layer to ‘Foreground’, add a Box Collider 2D, a Rigidbody 2D, and a new script called ‘Powerup’. Edit the collider and make sure ‘Is Trigger’ is checked and ‘Gravity Scale’ is ‘0’.

Open the ‘Powerup’ script and let's begin to construct our powerup. We will need a serialized private float for ‘_powerupSpeed’ and set it to 3f. We’re going to have our powerup move down the screen like the enemies. This will be very similar to that script. We will access the powerup transform.Translate, use Vector3.down and multiply that by _powerupSpeed and Time.deltaTime. If our position reaches less than -5f then we will destroy the object, as we don’t need it respawning each time it hits the bottom of the screen.

Now, we need to build the collision of the object. We will create a new function ‘OnTriggerEnter2D’. Let’s rename the Collider2D to ‘other’. We’re going to make an if statement checking if the object collides with the ‘Player’ tag. If it does, then we will destroy the powerup object. We will add more logic here shortly, but we will have to build on this in another script.

We now need to open the ‘Player’ script and create a new function called ‘TripleShotActive’. In here we will set ‘_isTripleShotActive’ to true.

We will also create a simple coroutine and name it ‘TripleShotPowerDownRoutine’. In here we will yield and return our WaitForSeconds and set it to 5f. Then when that is complete we will set ‘is_TripleShotActive’ to false. We will have to initialize the coroutine back in the ‘TripleShotActive’ function.

Go back to the ‘Powerup’ script and in the ‘OnTriggerEnter2D’ function. In the if statement we need to reference the ‘Player’ script. We will create a new Player object and name it ‘player’ and attach the ‘Player’ script component to it. We will add a null check also and make sure the game has a player. In the null check, we will activate the TripleShotActive function from the player script. Our function is complete and looks like this.

We’re mostly done. As of right now, we have the powerup fully functional. When you collide with the powerup it will turn on the triple shot, after 5 seconds, it will turn off. The only thing left is to spawn the powerups. So let’s tackle that.

Spawning our powerup is almost exactly the same as the enemy spawner. We will need to create a new serialized GameObject named ‘_tripleShotPowerupPrefab’. We will also create a new coroutine called ‘SpawnPowerupRoutine’. The setup for this is exactly the same as the enemy spawn except for the timer. The timer we will set a random range between 3f and 7f. At Start, we will need to initialize the coroutine, just like the enemy spawn.

Now the spawner is done and we have a reoccurring powerup spawner.