Making the PowerUp System Modular
In the last two articles, we created a PowerUp for the Player to shoot 3 Lasers instead of 1 and set it up to spawn with the Spawn Manager. In this article, we will make the PowerUp script modular instead of making each PowerUp a separate script that does the same thing and add two more PowerUps.
Modular PowerUp System
Before we make the new PowerUps, we will set up the PowerUp script to be modular to add to those new PowerUps right away for testing their functionality.
In the PowerUp script, create a private int named “_powerUpID” with a value of 0, and make sure to serialize the variable so you can change it in Unity. Above the variable, you can add some pseudo-code to define what value equals which PowerUp.
In OnTriggerEnter2D, inside the player null check, create a series of if and else if statements for _powerUpID. If the value is 0, activate Triple Shot, else if the value is 1, activate Speed Boost, else if the value is 2, activate Shield, and finally an else to catch any other value. With that, the PowerUp script is ready for when we create the other PowerUps.
Next, in the SpawnManager script, comment out the triple shot prefab variable because an Array of type GameObject will replace it. An Array can store multiple variables of the same Type, and an Array can be of any type. It can even be an Array of Arrays. Creating an Array is similar to creating a standard variable; you start with the Accessibility, followed by the Type, then you add brackets after the Type to make it an array and finally give it a name. Arrays keep track of the objects in them by a number starting at 0. To retrieve one, you need to use arrayName[#]. Now create a private array of type GameObject named “_powerUpPrefabs” with a value of null. That will hold all of the PowerUp Prefabs so we can select one from it and instantiate it.
In the SpawnPowerUpRoutine, change the Instantiated gameobject to _powerUpPrefabs[#]; for the object’s number to be spawned from the array, have it be a random range between 0 and _powerUpPrefabs.length, which is the number of objects in the array. That will pick a randomly spawned PowerUp Prefab each time.
Speed Boost PowerUp
Create the Sprite gameobject, animate it, add Collider and Rigidbody, add the PowerUp script, change Power Up ID to 1 and make it a prefab like the Triple Shot PowerUp.
In the Player script, add a private bool named “_isSpeedBoostActive” with a value of false, a private float named “_speedMultiplier” with a value of 2, and a private float named “_speedBoostDuration” with a value of 5.
In CalculateMovement, create an if and else statement where if the speed boost is active _speed will be multiplied by _speedMultiplier, if not move at normal _speed.
Then create a new public method named “SpeedBoostActivate” and inside of it, create an if and else statement that checks if _isSpeedBoostActive is false and if so set it to true, else increase _speedBoostDuration by 5. Next, create a new Coroutine named “SpeedBoostPowerDownRoutine”. Inside it, make a while loop that will run as long as _speedBoostDuration is greater than 0, have it yield for 1 second, and then decrease _speedBoostDuration by 1. After the while loop has been completed, set _speedBoostDuration back to 5 and _isSpeedBoostActive to false. Finally, start the Coroutine in SpeedBoostActivate after setting _isSpeedBoostActive to true.
In the PowerUp script, change the pseudo-code for activating Speed Boost to calling the SpeedBoostActivate method on player.
Now to hook up the Prefabs on the Spawn Manager. Increase the Power Up Prefabs array size to 2 and drag in the Triple Shot Prefab and the Speed Boost Prefab.
Both PowerUps spawn randomly and work as intended.
Shield PowerUp
Create the Sprite gameobject, animate it, add Collider and Rigidbody, add the PowerUp script, change Power Up ID to 2 and make it a prefab like the other PowerUps.
In the Player script, add a private bool named “_isShieldActive” with a value of false (for turning on and off the damage blocking / Shield visual), and a private GameObject named “_shieldVisual” with a value of null (the gameobject with the shield graphic on it).
Next, in Start, turn off the _shieldVisual with SetActive(false). That will deactivate the gameobject. When the gameobject is deactivated, the components are disabled, including the Sprite Renderer making it invisible.
Then create a new public method named “ShieldActivate”, and inside of it, create an if statement that checks if _isShieldActive is false. If so, set it to true and turn on the _shieldVisual with SetActive(true), re-enabling the Sprite Renderer giving the illusion the Shield just turned on.
In the ChangeLives method, before _lives is changed, create an if statement to check if _isShieldActive is true. If so, make it false, turn off the _shieldVisual with SetActive(false) to give the illusion the Shield was used and disappeared, and tell the rest of the method not to execute by using return. The return keyword tells the script that the method is done and continues with the frame.
In the PowerUp script, change the pseudo-code for activating Shield to calling the ShieldActivate method on player.
To make the Shield visual create a new Sprite gameobject named “Shield Visual” as a child of the Player, drag in the first frame of the shield sprite sequence, then animate it and drag the Shield Visual gameobject into the corresponding gameobject field on the Player. With the Shield Visual being a child of the Player, it will always move with the Player.
Then on the Spawn Manager, increase the Power Up Prefabs array size to 3 and drag in the Shield PowerUp Prefab.
With that the all the PowerUps are spawning and working.
Quick Clean Up
Now for a quick clean-up of the code.
First, change the _powerUpDuration variable to “_tripleShotDuration”. Below is a quick way to rename a variable or method in Visual Studio that will go through the project and change the name of all its references.
Next is a way to give a little tip about what a variable does in Unity. Instead of just using comments in the PowerUp script to show the different values of _powerUpID, using [Tooltip(string)], you can tell Unity to display a popup when you hover over the variable in Unity.
Finally, it is always good to stay organized. Create some folders in the Prefabs folder to keep similar prefabs together.
The following article will look at a switch statement and an enumeration type for the PowerUps.