Power Up System in Unity — Part 2
Ok! This time, let’s go over how we can spawn TripleShotPowerUp similar to how the enemies are being spawned + I will show you a simple animation that will add quality to the game!
To spawn TripleShotPowerUp, let’s first open up SpawnManager script. It’s going to be really really similar to how we did for Enemy by using StartCoroutine() with IEnumerator.
- At the top of the script, let’s create a variable for PowerUp prefab that we can use.
- Under void Start(), we will create StartCoroutine(SpawnPowerUpRoutine());
- Then, we will create a new method with IEnumerator that receives SpawnPowerUpRoutine() from Coroutine.
- Use while loop that will activate while ‘bool _stopSpawning’ is false (it will turn to ‘true’ when the player is dead). Under while loop, follow the steps below.
- create a variable that will get a random X position value between -5 to 5 (left and right ends of the game screen). Then, use it to instantiate the PowerUp prefab.
- create another variable that will get random seconds between 5–10, then use it to WaitForSeconds(). This will spawn the PowerUp prefab at random timing and not too consistent.
Power Up → ‘_isTripleShotActive’ to TRUE
Ok, in the previous article, in PowerUp script’s OnTriggerEnter2D(), I realized that I did not make it clear how we are calling the player script and changing ‘bool _isTripleShotActive’ to TRUE (so that we can shoot Tripleshot).
Here are what I did:
- In Player script, create a new method called ‘TripleShotEnabled()’ that will turn ‘bool _isTripleShotActive’ to TRUE.
- In PowerUp script, when collided with the player, get player script by ‘GetComponent<>’. Then, if found, call this ‘TripleShotEnabled()’ method.
Now, we need to limit the time so that the player can shoot tripleshot for few seconds only, then return to a single laser shot.
- In Player script, we previously implemented FireLaser() method so that we can activate either tripleshot or singleshot. We just need to add Coroutine that will count 5 secs, then disable Tripleshot.
Bonus — Animated PowerUp Prefab!
Let’s add some spice to TripleShot PowerUp. The current one looks a bit plain, but what if we add an effect of flashing colors? Here what we need to do.
- Prepare a series of sprites with different colors — This can be done in photoshop, illustrator, etc. Luckily, I have these sprites ready to be used!
2. Select TripleShotPowerUp Prefab, then go to Window → Animation → Animation. This will open an animation window. Click on ‘Create’ when the window pop up.
3. It will take you to a folder to save this animation file. Create a new folder called, ‘Animation’. Then, save it as ‘TripleShotPowerUp_anim’.
4. Click on the record button at the top left (red circle). The red bar will show up on the timeline to indicate that we are in record mode. Select all of the sprites that we want to animate with. Then, drag and drop into the timeline window. Press the record button again to stop the recording mode.
That’s it! Now, we have the animation set up. Simple and Easy, right?
The game is looking better and better! Awesome!
Thank you so much for reading!