In this article, we will be adding a new secondary fire powerup. I’m wanting to create a spray shot that will shoot three lasers. One laser will be shot forward, while two other lasers will be shot out diagonally from the left and right wings. Let’s get started.
Let’s create a new empty game object in the hierarchy and call it ‘Spray_Shot’. Let’s drag in three laser prefabs and make them children of the spray shot game object. Position them how you want them, I have the two lasers on the wings, going outward so when they are shot, they will spread out and potentially hit enemies that are outside of where the player is at.
In our ‘Player’ script we need to create a new serialized GameObject named ‘_sprayShotPrefab’. We will need to drag the prefab we just made into this variable when we go to Unity next. We will also create a new private bool called ‘_isSpreayShotActive’ and set it to false.
We now want to create a new coroutine called ‘SprayShotPowerDownRoutine’. We will want to wait for 5 seconds, then set our bool to false. Then, we need to make a new public function called ‘SprayShotActive’ and set the bool to true, then start our coroutine.
This next part could probably be cleaned up a little better. I actually was having some issues getting the triple shot to not shoot a regular laser when it was active, so I had to do a little bug fixing at the same time.
In our ‘ShootLaser’ function we need to extend our if statement out some. But first, at the top of the if statement with the triple shot, we need to declare that the spray shot is false. Then we want to write out an else if statement stating that if ‘_isSprayShotActive’ is equal to true, then ‘_isTripleShotActive’ is false, then we Instantiate the ‘_sprayShotPrefab’ the same as the triple shot.
I did it this way to verify that the other powerups we’re going to hijack the event.
We will now create a powerup just like we had in all the other articles. We need to make sure to set the Powerup ID correctly and update the SpawnManager script and game object with the powerup prefab. We also need to add another case in the ‘Powerup’ script to represent the ‘SprayShotActive’ function we wrote earlier. By this point, I’m just repeating myself, so those were the highlights of what needs to change for this new powerup. Start your scene and check out your new spray shot.