Adding a burst laser powerup
We’re going to implement a sort of star burst secondary fire attack for the player.
To get started, we create a LaserBurst game object for parenting and using our normal red laser sprite changed to the color blue to create 5 child game object lasers.
First we create a parent object, name it LaserBurst and create a new script called “LaserBurst” for it.
I’ve copied over the contents of our existing Laser script to give us a starting point but this script will be very different as it will control all 5 child game objects simultaneously.
So, we have a central placed laser and 2 lasers to each side with some varying size and rotation.
Make sure to set the tag for each child game object laser to “Laser”.
We’ll need to add some components to each laser child game object.
Add a BoxCollider2D component and a Rigidbody2D with the necessary settings as shown above to each laser.
The box colliders on my lasers were all oversized by default, use the Edit collider as needed.
When on the player game object, it should look something like this.
When you’re happy with everything, create a LaserBurst prefab.
Building out our LaserBurst script
The first thing I’m going to do with our new script, after copying over the contents of the Laser script, is to implement a way to track all of the child objects.
So let’s add an array variable to store those game objects.
Assign the child laser game objects to the LaserGOs array on the LaserBurst script.
If you haven’t created a prefab, go ahead and do so for LaserBurst.
If you have, make sure to apply the overrides.
I’m going to start by adding a DoNullChecks() method and calling it from the Start() method.
We use the foreach statement to iterate through our _laserGOs() array and null check each child game object as well.
Using a counter variable, we can print a debug log that is more specific when a null game object is found.
Above DoNullChecks() method I’ve added a MoveAllLasers() method and called this method in the Update() method.
The MoveAllLasers() method will loop through each child laser game object and move it up on the screen.
Once any of those child game objects reaches a position.y value far off screen, we delete this game object and all child game objects with it keeping our hierarchy view clean and tidy.
Modifying the Player script
Let’s add variables for our LaserBurst prefab game object and set the fire rate the same as our normal fire rate for now.
Go ahead and drag the LaserBurst prefab game object onto the Player prefab game object’s Player script component.
We’ll update the FireLaser() method to check for the _laserBurstActive first, then _tripleShotActive, and then our else statement for standard fire.
I’m happy with the standard fire laser rate so I’ve reused _nextLaserFireTimeStamp in the _laserBurstActive section.
This means I will not need to alter the Update() method where we call fire laser and use the _nextLaserFireTimeStamp to do so in a timing fashion.
There is no need to reinvent the wheel here, we use the TripleShotActive() method and TripleShotPowerDown IEnumerator as a template to create our LaserBurstActive() method and LaserBurstPowerDown() IEnumerator.
Remember to declare them as public so outside scripts can call on them.
We still want a 5 second cooldown, so the value in WaitForSeconds() need not be changed.
Creating the Secondary Fire powerup
Now, for our Laser Burst (Secondary Fire) ability to ever happen, we will need a way to trigger it.
We’ll do this through a powerup that spawns more rarely than all the others.
We’re going to base this powerup on the Ammo Charge powerup we did before because I want to use a sprite image multiple times to represent this powerup.
We add our CircleCollider2D(), is trigger to true, Rigidbody2D, Gravity Scale to 0, Powerup script, and Audio Source.
Make sure to set “Powerup ID” on the Powerup script component to 4.
We assign the power_up_sound audio clip to the powerup script variable for it.
Just to change things up, I’m going to add a halo effect to this powerup.
Now, I’ve combined a bunch of game objects with separate sprites to create this powerup.
Since we are just prototyping, not creating for production, this is okay.
However, if you are planning to release a game, it would be best to take the time and create a single sprite in the appropriate software and use it on one game object.
We’ll create the powerup by creating 5 image game objects using the same sprite and then resizing and repositioning them.
You should end up with something similar to this:
Modifying the Powerup script
We update our Case 4: statement to call our player script and set the burst laser as active.
Because we have a halo effect on the powerup game object, we use this opportunity to disable the game object and hide the halo immediately.
If we didn’t do this, the powerup icon would disappear and the halo would linger for a moment.
We also change our if statement to < 4 now that we know our case elements of 4 and above have multiple child objects with sprite renderers on them.
Modifying the SpawnManager script
Let’s add a variable for our _powerupLaserBurst prefab game object.
We add another coroutine for the BurstLaser based on the others.
Last we update our StartSpawning() method to call on our new coroutine and give it the largest minimum and maximum spawn intervals.
Let’s drag our BurstLaser_Powerup game object prefab onto the SpawnManager script component.
And that’s it, give it a try!