Creating a Shield Power-Up in Unity3D

Pete Thomasberger
4 min readMay 22, 2021

--

Objective: Create a Shield power-up that will trigger a shield visual that surrounds the Player GameObject and allows the Player to take one damage without decreasing the Lives value.

Step 1: Create Shield_Power_Up Prefab

Using the Shield Sprite from GameDevHQ, create a Shield_Power_Up Prefab.

Reference this article for instructions for creating a power-up Prefab: https://petethomasberger.medium.com/dev-day-32-creating-modular-powerup-systems-in-unity3d-e6a10051a89d

Note: Since this is the third power-up in our game, the PowerUpID for the Shield_Power_Up GameObject will be 2.

Step 2: Assign the Shield_Power_Up Prefab to the SpawnManager Array

Select the Spawn_Manager GameObject and in the Inspector window, add Element 2 in the Power Ups Array section of the Spawn Manager (Script) component. Then drag and drop the Shield_Power_Up Prefab into the Element 2 slot in the Power Ups Array.

In the SpawnManager script, in the PowerUpSpawnRoutine() method, change the max value for the range of the randomPowerUp variable from 2 to 3.

Step 3: Activate the Shield Power-Up

In the Player script, create a private bool called _isShieldActive, which is false by default.

Create a new public method called ShieldActive(). Then call the _isShieldActive variable and set it to true.

In the PowerUp script, in the OnTriggerEnter2D method, add a new case to the switch statement with 2 as the case ID. Then call the ShieldActive() method from the Player script.

Step 4: Update the Player’s Damage() Method

In the Player script, in the Damage() method, create an if statement at the top of the method, stating that if _isShieldActive is true, do not damage the Player. When this if statement is called, _isShieldActive will need to be set to false since the shield will only protect the Player for one collision. Then add return at the end of the if statement so the method will restart and not call the rest of the statement in the method. This will allow the Player GameObject one collision without subtracting a life.

Step 5: Create Shield GameObject

In Unity, using the Player_Shield Sprite from GameDevHQ, drag a frame from the Project window to the Hierarchy window and drop it on top of the Player GameObject. This will set the Shield GameObject as a child of the Player GameObject, allowing the Shield visual to always attach the Player’s position while _isShieldActive is true.

Step 6: Activate and Deactivate the Shield GameObject

To activate and deactivate the Shield GameObject so it is hidden when _isShieldActive is false and visible when true, go to the Player script and create a new private GameObject variable utilizing [SerializeField] and call it _shieldVisual.

In Unity, select the Player GameObject, then drag and drop the Shield GameObject into the Shield Visual slot of the Player (Script) component in the Inspector window.

In the Player Script, now that the Shield GameObject is referenced, use the SetActive function to activate and deactivate the Shield GameObject.

In the ShieldActive() method, use _shieldVisual.SetActive(true) to activate the Shield GameObject when _isShieldActive is true.

In the Damage() method, within the if statement, use _shieldVisual.SetActive(false) to deactivate the Shield GameObject when _isShieldActive is false.

Lastly, in the Start() method, add _shieldVisual.SetActive(false) to ensure the Shield GameObject is not active at the start of the game.

Save the script, go back into Unity, play the scene, and the Shield power-up will spawn randomly with the other power-ups. Once the Shield power-up is collected, the Shield will be visible, and the Player will be allowed one damage-free collision.

--

--

No responses yet