Day 18: Starting to Feel Like a Real Video Game!
Objective:Add triple shot powerup!
Now that we added all the art it’s time to make the game for fun!
First thing first is to create a prefab that resembles three lasers being fired.
To do this right set the player to 0 on x, y, and z axis.
Add three laser prefabs to the project. And position them where you would like them to come from.
We got the lasers where we would like them to be spawned from. Now instead of instantiating three objects individually, we’ll turn them into a single prefab by creating a Empty game object and resetting it’s position to zero.
You want the position of the triple shot powerup at zero because when we instantiate the game object with an offset, it’ll add the values that the game object has.
Now add the three lasers as a child to the power up.
Take the powerup and create a prefab. Once that is done we will go into firing the triple shot. In the player script we will check to see if triple shot is active and if so fire the triple shot. If not fire just a single laser.
We have the logic to fire the triple shot now. Next will be to turn the _isTripleShotActive bool to true. Since we don’t want to change the variable directly we’ll create a public function called TripleShotActivate, and set the bool to true.
Next we will need to create a coroutine to power down the powerup. Below TripleShotActivate function create an IEnumerator called TripleShotPowerDownRoutine. In it you will do a yield return new waitForSeconds(). I’ve created a global variable to be adjusted in the Inspector. Next is to have the bool turned back to false. Now the IEnumerator is created, don’t forget to start the coroutine by adding StartCoroutine(TripleShotPowerDownRoutine()) in the TripleShoteActivate function.
Now to call this when we need to. We’ll create a script called Powerup. On this we will need the logic to move down and collision to see if it’s the player. If it is and the player is not null, call this function. As well as destroy the game object that’s holding this script.
We have the logic, next will be putting it on a game object and creating a prefab out of it.
This object will needs a collider. Any collider will do fine, as long as it’s a 2D collider. Make sure trigger is checked. Next add a rigidbody and turn gravity to 0. To finish it off add the powerup script.
The powerup for tripleshot has been created! Last thing we will talk about today is to spawn the powerup. In our spawnmanager we need to set up the logic. We will create another coroutine for spawning powerups. This will be almost similar to the enemy coroutine.
First will be to have a reference to the powerup prefab.
Now to instantiate it above the screen while the player is still alive. We will have it spawn between 3 and 7 seconds. When using just int’s in Random.Range, always add a number. The max that is inputted will be excluded. In the code I put min is 3 and Max is 8. The code is subtract one and it will really be between 3 and 7.
That is it! We have a powerup that will spawn between 3 and 7 seconds that will active the triple shot for the player to fire.