2D Galaxy Shooter — “Triple Shot” Powerup using Sprite Animation and Coroutines
Hello all!
Today, I will be going over how to make a “Triple Shot” powerup for our player. To do this: we will need a sprite to activate the powerup, the Spawn_Manager to spawn the sprite, then set what happens when it’s collected.
Making the Fire Formation using a Parent
- Add lasers wherever you have gun spots. In my case, I have spots to shoot lasers on my wings.
- Remember to check your Z axis, it should always be 0. - Once you have them in the correct spot, create an Empty game object, name it “Triple_Shot”, and set its position to (0, 0, 0). If it is not set there first, your lasers won’t shoot from your desired locations while moving.
- Find the three lasers and select them from the hierarchy.
- Drag and drop them on to the new “Triple_Shot”. This will make the lasers children of the empty.
- Make the “Triple_Shot” a prefab by dragging it into the prefabs folder.
- To clean up the hierarchy, head to the laser script and add this if statement above the Destroy(this.gameObject);. This will destroy the parent gameobject and keep the hierarchy clean.
Making a Powerup Sprite with Animation
- Just like in my last article, you will need to find a .PNG to set as your “Triple Shot” powerup sprite, name it Triple_Shot_Powerup.
- If you have the assets and want to make an animated sprite, continue. If not, then head to step 9.
- Open the Animation window by going to “Window”, “Animation”, then “Animation”. You can use ctrl + 6 too.
- Add a sprite on to the scene view and select it.
- While looking at the Animation window, hit the create button. You will want to save this file in a new folder named “Animations”, then name the file “Triple_Shot_Powerup_anim.anim”.
- By adding the “_anim” at the end, that will make your animations easier to find once you have hundreds of assets to look through. - Press the red record button at the top of the animation screen.
- When recording, you can now add the images you want to the Dopesheet.
- To test it, press the play button and watch it run.
- Add a “Box Collider 2D” component and set it to “Is Trigger” and set the hitbox how you want it.
- Add a rigid body component and set the gravity scale from 0 to 1.
- Make the “Triple_Shot_Powerup” a prefab by dragging it into the prefabs folder.
Adding the Powerup to our Spawn_Manager
Now that we have the sprite made, let’s add it to the Spawn_Manager.
- Head to the SpawnManager script and make a private variable —
[SerializeField] private GameObject _tripleShotPowerupPrefab;
and link the Triple_Shot_Powerup prefab to the Spawn_Manager. - Now add the method above. It’s similar to how we spawn enemies but this time, we don’t need a container and we are spawning powerups at a random range between 3 and 7 seconds.
- In the Start method, add “StartCoroutine(SpawnPowerupsRoutine())” right under the one for enemies.
When the Player gets a Triple Shot Powerup
- Head to the Player script
- Make variables—
[SerializeField] private bool _isTripleShotActive = false;
[SerializeField] private GameObject _tripleShot; - Set the Triple_Shot laser to _tripleShot on the Player script component.
- Find the FireLaser() method and add the code above. What‘s already there can go into the “else” statement. This is saying that if _isTripleShotActive is true, then shoot triple shot, but if it’s false, then fire the regular laser.
5. Make two new methods as seen above. This is saying that when Triple_Shot is active, start the Coroutine below for 5 seconds. After the 5 seconds are over, change the _isTripleShotActive to false, ending the powerup.
Powerup Behavior
Make a new C# script and name it “Powerup”, then add it to the Triple_Shot_Powerup.
- Create a variable — [SerializeField] private float _speed = 3.0f;
- Add the code above in Update. This just means that when spawned, the powerup will go down at a speed of 3 till it leaves the screen, then is destroyed.
Now let’s make a OnTriggerEnter2D method:
This is saying that when it collides with something with the tag, “Player”, then run “ActivateTripleShot” on the player script, allowing the player to fire the triple shot!
This has been how to set up a Triple Shot powerup that is animated!
Next, I will make a speed boost powerup using a switch statement!
Thank you for your time!