Starting to feel like a Real Video Game
Objective: Implement a triple-shot power-up.
Now that the cubes and capsules have been swapped out and some nice visual assets have been added to the game, its starting to fell a bit more game like. The only problem is if this was all it was, it would get boring very, very soon.
This is where some added features will make the game more appealing and exciting. In this post we’ll add in a power up feature and then add some more powerups, sound and UI later on. Let’s get started with the triple shot.
To start, create an empty object in the hierarchy and rename it tripleshot. Then drag the laser prefab into the object and make 2 more copies of the laser prefab. Move one to the left side of the player ship over the wing and the other over the opposite wing. Drag the object into the prefabs folder in the project section to prefab the triple shot prefab and delete the one left in the hierarchy.
Create a serialized gameobject called tripleshotprefab to set the prefab in the inspector. Create a bool to check if the triple shot is active and set it to false as default then create an if statement within the shoot laser method checking if the triple shot is active. It will be if active shoot triple shot else shoot single laser. For now make sure the is triple shot active is a serialize field for to test when its active and not.
Your triple shot alignment may be off when you shoot. If so open the triple shot prefab and reset the position points then set them according to your players alignment.
Next drag the triple shot sprite into the hierarchy and scale it to the size you want to use. Rename it so you can call it later, add a 2D collider(i’ll use a box), set the collider sides by editing the collider, add a rigidbody 2D and set the gravity to 0. Once all that is set, create a C# script called power up and attach it to the triple shot prefab in the hierarchy.
Inside the Powerup script, create a serialized speed float(set to 3f). Inside the update method set the transform to translate down * speed * time. Create an if statement checking if the powerup has left the bottom of the screen and if so destroy it. Create an on trigger enter 2D method and check if the player tag hits the powerup and if so destroy the powerup.
Next go to the spawn manager script and create an IEnumerator for the powerup. Like we did in the enemy coroutine, use the same logic, while spawning, instantiate a powerup at a given position(with a random x-axis) but this time with a random time range between 5 and 8 seconds between spawning. Set the coroutine to start on game start and create a serialized game object to assign the triple shot powerup prefab in the inspector.
Next to activate the triple shot when the player collects the powerup go to the player script and create a public triple shot activate method and use it to set is triple shot active to true.
Now go to the powerup script and in the on trigger enter method set a Player variable and assign it to the player component then call the triple shot activate. Make to call it before destroying the powerup or it will never make it to that call as the gameobject will be destroyed.
In a future post we will create a cool down function to turn the powerup off after a specified time but first in the next post, we will look at some animation for the game assets we have already working.