Player movements, enemy hordes, weapons, and basic artwork, with all this coming together it really does feel like a real video game. We still have a few ingredients missing, so let's start by adding one such ingredient.
Power-ups are basically an object or an item that upon consumption grants the Player a temporary/permanent skill or an ability. So let’s add one such ability to our Player.
To make this Triple-Shot the Laser prefab was reused and placed in their respective positions and were parented to an empty game object.
This TripleShot game object contains our Laser behavior attached to it. In other words, only the TripleShot has the Laser script attached to it, which makes the Laser travel upwards and destroy itself once it goes off-screen. This is now made into a prefab.
With this done we can now move on using a triple-shot power-up which upon contact will enable us to use the newly created triple-shot.
I’ll be using this asset that was provided by GamedevHQ. This contains a Box Collider 2D component with Is Trigger toggled ON and a Power-Up Script which contains its behavior of traveling downwards and giving the player the ability to fire a Triple Shot.
Let's start with the movement.
This method takes care of the behavior of traveling downwards and destroying itself once it's off-screen.
Now let's look at how this object reacts when it comes in contact with the player.
Here, we’re using the OnTriggerEnter2D(). If the object that collides with this object has a tag called Player, only then does it execute the command that follows. It’ll not only destroy itself but also let our Player fire the Triple Shot projectile. Here _player is a variable of type Player. This lets us access all the public methods and variables present in the Player script. To learn more in detail regarding Triggers and GetComponent you can refer the previous articles which explain them in detail.
Now let's look at the Player Script to learn how to use the power-up for a limited amount of time and since we are dealing with keeping something active for a time period we can use a Coroutine.
IsTripleShotActive() is as follows,
IsTripleShotActive() is a placeholder method whose functionality is to run our specified Coroutine which in this case is the PowerEffectLast(), which when called sets our boolean variable to true for a specified amount of time and then sets it back to false.
_isTripleShot boolean is then used to decide if we are to fire a normal projectile or our powered-up projectile. This is done by,
This Method takes care of firing the Laser, here InstantiateLaser is a method that does the following
This takes care of instantiating the game object which is passed in as a parameter to this method, upon completion we parent the instantiated object to an empty game object to clean up our Hierarchy panel.
This gives us the following result.
Thanks for reading. More to come!!!!