Space Shooter: Triple Shot Power Up
Right now I’ve got a cool little laser that I can fire and its all good, but I wanna get weird, so I made a triple shot power up.
I made this power up by using three of the laser prefabs. I dragged them into the Hierarchy and then positioned them appropriately. Then I made an empty game object and placed them inside it. (Make sure the parent object is set to 0,0,0 before putting them inside).
In my Fire method I have simply made logic to handle instantiating the game object
I assigned it in the inspector and its all good.
The reason it is all good is because its made up of three laser prefabs already. Those prefabs already have the logic for how a laser should behave. It moves up indefinitely until reaching a certain y axis value or when it collides with an enemy.
Spawning the item that gives you the power up is handled in much the same way as spawning the enemies is.
I have them spawning just like the enemies, but they go in their own container called PowerUp Container.
My power up is animated. The power up item I have has several frames included with it. To animate it I just drag the first frame into the hierarchy and then find the “Animation” window.
I drag all the frames into it and adjust the keyframes accordingly.
I also added a circle collider onto this game object. DONT FORGET TO MAKE IT A PREFAB AND ASSIGN IT IN THE INSPECTOR OF THE SPAWN MANAGER FROM THE PROJECT VIEW.
I also needed to make a PowerUp Class to handle collision with the player and the power up’s movement.
The movement we all know how to accomplish already. If you’re stumbling upon this article and are not sure how to handle making things move in Unity feel free to check out some of the previous articles!
Collision is something we went over, but I’ll cover it briefly again. For this We want something to happen when it collides with the Player, so the OnTriggerEnter2D function has an if statement that handles that.
This means we need to make logic in the player class.
When the power up collides with the player it called the TripleShotActive function. This sets
isTripleShotActive to true. Then it starts a coroutine. After fiver seconds the
isTripleShotActive bool gets set back to false.
This is what the Fire method code means. I have a condition that says if tripleShotActive is true then instantiate the triple shot. If not fire the regular laser.
So after we have all this logic set up and play the game:
This logic will be applied to all the other power ups I decide to set up. I’ll get into how to make this type of thing a bit more modular when I reveal some of the other power ups. Until next time!