We’ve created our TripleShot Power-up in a previous article but I had to manually activate it in the Inspector. Today we’ll be creating the actual power-up that will periodically spawn and fall downwards in a similar way as our enemy spawns. In fact, the system to spawn the power-ups will be very similar to the enemies.
Creating the Power-up
In the assets folder that we imported from Filebase is a folder called “Power_Ups” which can be found in the “Sprites” folder.
Instead of creating a new object in the Inspector, we’ll be dragging the first sprite frame from the “Triple_Shot” folder into the Hierarchy. If the power-up is somewhat larger than our player then all you need to do is make adjustments on the x, y and z Scale on the Transform of the object. I’ve also renamed the Power-up to “TripleShot”.
Animating Sprites in Unity
You may have noticed that our “Triple_Shot” folder contained quite a few sprites. When we combine those sprites using Unity’s Animation tools we’ll create amazing effects that will bring our game to life.
What we’ll need to do is open up the Animation and Animator windows by going to “Window” then “Animation” and then opening the windows. I’ve snapped the Animation window next to my Game window and snapped the Animator window next to the Console window.
Now we’ll be adding an animation controller to the Tripleshot. First, make sure your TripleShot power-up is selected in the Hierarchy. Next click on the “Create” button in the Animation window. You’ll be prompted to save your newly created Animation. I’ve already created a folder called “Animations” and saved my animation controller and the default animation. I won’t be going into detail about how different animation states are working and how to activate them, we’ll be covering that in a future article.
Creating the Logic
Before creating the logic we need to understand what we want to achieve and code the logic accordingly.
- The SpawnManager will periodically spawn new power-ups
- The power-up will trigger a public function on the player
2.1. The function will enable the TripleShot power-up
2.2. The function will also start a CoRoutine with a 5-second cooldown
- The power-up will be destroyed.
We already have a player script so we can proceed to open the script. I’ve created a new function that’s easily recognizable when we need to refer to it so I called it “ActivateTripleShot()”. Inside that function, we can now set the “_tripleShotActive” to true. Next, we’ll be creating a new CoRoutine called “TripleShotCooldownRoutine()” and create new a 5-second cooldown whereafter it will turn the “_tripleShotActive” to false.
The Power-up script
In the Scripts folder, I’ve created a new script called “PowerUp” and dragged it to the TripleShot object in the Hierarchy. At this point, we can create a prefab in the Prefabs folder simply by dragging the TripleShot object into the folder. At this point, you can delete the TripleShot object from your Hierarchy since we’ll be instantiating it from code.
Similarly to what we covered in the enemy logic, I’ve allowed the power-up to move in a downwards direction where the player can interact with it and obtain the powerup. But unlike the enemy, we don’t want the power-up to appear at the top of the screen when it’s left the bottom bound of the screen and therefore I chose to destroy it instead.
Next, I’ve created another “OnTriggerEnter2D” function where I check when the power-up collides with a player, that it activates the TripleShot power-up.
The SpawnManager Script
We’ll be working on the SpawnManager script we’ve created before.
I created another IEnumerator called “SpawnPowerUpRoutine()” where I used the exact logic as before, using a while loop, and checking if the “_stopSpawning” bool is false before running through the code. Later on, we’ll be revisiting this function since we’ll have 4 or 5 power-ups with unique logic and we’ll modify the code to randomly spawn a different power-up possibly using random number generation (RNG) to create common or rarer power-ups.
All Wrapped Up
Everything is working as intended. Personally, I feel the power-ups are spawning too often, but for the sake of testing the game, I’ve decided to leave it as is until I’ve added more power-ups.
Thanks for reading and till next time, happy learning.