120 days to master Unity (Day 4) -Shields up! Red Alert!

Brian Perry
5 min readMay 1, 2021

--

Today, I’m going to talk about including the shield logic. The shield game object is going to be a power-up like all the other power-up. This means is going to use the power-up script again. To start out, we find a 2D asset and drag it into the Hierarchy view in Unity. In my case, I have an entire animation for the shields and I can drag the first frame of the shields into the hierarchy view to get started. Any 2D art that you have can be used to stand for the shields by the way.

From here, its just a matter of setting up your new game object. In order for the powerup game object to work, it needs to have a collision component and a rigid body component added to it.

Click on Add Component and select rigid body 2D and BoxCollider2D. For rigidbody2D, make the gravity property is zero. For BoxCollider2D,make sure Is Trigger is checked.

Now that the right components are added to the shield powerup game object, we can just drag the powerup script into it to and viola, we have instant logic.

After we do this, now all we have to do is put the right logic into the powerup script so that our powerup script will work when the player object collides into it.

In the last article, I added logic so that the collision will call a powerup in the player script depending on what the player collided into. This is done by adding logic that detects the powerupID of the game object that has a collision with the player object. When the player game object collides into the game object with powerupID of 2, which is the shield game object, it will enable the shields powerup in the Player script. Also, don’t forgot to drag the shield powerup prefab into the powerup prefab slot.

Next, if you want your shield powerup to be seen, you have to tell spawn_manager how to spawn it. This has to be done in the spawn manager.

I spawn everything independently in my spawn manager and put them all inside of the start method. As you can see from the above picture, I use coroutines to add pauses between the spawns.

The coroutine method I use pauses between spawns. I’m using a variable to control the pauses between spawns called timeBtwPowerupSpawns. I use [SerializeField] above when I declare it so that I can change the value in the inspector. Also, the game object can now be dragged from the hierarchy view to the prefab folder in project view.

After doing this, you need to add in the effects of this powerup. If you don’t, you just have a collectable powerup called shields that doesn’t really do anything. With that, the last thing to do is make sure the player is not damaged if the player is hit while the shields are powered up.

Like I did above, I had to change the script so that if the shields are active, the player doesn’t take damage. Also, the shield powerup is destroyed after they are hit so shields are immediately disabled after they have a collision. You could also tweak this and make it so that the shields just stay on for a certain time like the other powerups too. Its also possible to have the shields take a certain amount of damage before they are destroyed. Those could be future things I could put into the game.

The very last thing I need to do now is show the shield around the player when the player collects the shield powerup. This can be done by creating a shield game object that represents the graphical shield the player sees on the screen.

I am able to generate the functionality of the shields being able to shrug off at least 1 hit from an enemy by modifying my damage method in Player. I set the shield active Boolean to true if the player collects the shield powerup. After that, I modify my player damage script so that if shields are enabled, the script simply skips assigning damage to the player for 1 hit. I don’t have to do it this way. I could set the shields on a timer also so that the player can take infinite damage for a certain time until the powerup time runs out. That could be a future game feature. To show the visual part of the shields, I can just add a 2D object to represent the shields to the player’s ship as a child of the ship. This will cause the shield to follow the player as the player is moving around the screen. Also, since the shield prefab will be a child of the player’s ship, I can activate and deactivate the shield by just using a game object and using GameObject.SetActive(true/false).

--

--