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.

Triple Shot

Making the Fire Formation using a Parent

  1. 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.
  2. 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.
  3. Find the three lasers and select them from the hierarchy.
  4. Drag and drop them on to the new “Triple_Shot”. This will make the lasers children of the empty.
  5. Make the “Triple_Shot” a prefab by dragging it into the prefabs folder.
  6. 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

  1. 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.
  2. If you have the assets and want to make an animated sprite, continue. If not, then head to step 9.
  3. Open the Animation window by going to “Window”, “Animation”, then “Animation”. You can use ctrl + 6 too.
  4. Add a sprite on to the scene view and select it.
  5. 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.
  6. Press the red record button at the top of the animation screen.
  7. When recording, you can now add the images you want to the Dopesheet.
  8. To test it, press the play button and watch it run.
  9. Add a “Box Collider 2D” component and set it to “Is Trigger” and set the hitbox how you want it.
  10. Add a rigid body component and set the gravity scale from 0 to 1.
  11. 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.

Method to spawn powerups
  1. 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.
  2. 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.
  3. In the Start method, add “StartCoroutine(SpawnPowerupsRoutine())” right under the one for enemies.

When the Player gets a Triple Shot Powerup

  1. Head to the Player script
  2. Make variables—
    [SerializeField] private bool _isTripleShotActive = false;
    [SerializeField] private GameObject _tripleShot;
  3. Set the Triple_Shot laser to _tripleShot on the Player script component.
  4. 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.

  1. Create a variable — [SerializeField] private float _speed = 3.0f;
  2. 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!

--

--

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
Marcus Cohee

Marcus Cohee

20 Followers

I am starting Unity and learning the ropes as I go! Let’s take this journey together!