Made With Unity | 2D Space Shooter Part 8: Shields Up And Programing Tips

Rehtse Studio
5 min readMay 21, 2022

In this series of article / tutorials we are going to be making a small 2D Space Shooter using Unity3D and later the New Input System.

ADDING THE SHIELD POWER UP

SHIELD EFFECT

Make sure that the Player is on Position 0,0,0. Will drag the first sprite of the shield effect to the Hierarchy.

Rename the object to Shield and on the Sprite Renderer make sure that the Order in Layer is 5 also add a Animator component.

On the Animations folder create a folder called Shield, on that folder you will stored the ShieldController and the Shield_anim once is created.

Drag your Shield object to the player and turned it off

SHIELD POWER UP SPRITE

The step to make the power up sprite are the same that we had discussed in the past articles. I created the folder Shield inside the Animation->PowerUps->Shield to store the ShieldPowerUpController and animation; I drag the sprite to create a Power Up Shield object, created a prefab, added the necessary components and tag, created the PowerUpShield script and called for the shield method that is on the Player scrip.

Add the PowerUpShield to the SpawnManager on the SpawnPowerUps scrips field.

SCRIPTS

POWER UP SHIELD

When you pick up the power up it well call out the ActivteShields() method on the Player script.

PLAYER

Drag the Shield object to the Shield Game Object field in the Player script. When you the power up script called to activate shield the Shield game object will be enable giving the effect that it turned on, if you get hit you will loose the shield but not your life.

PROGRAMING TRICKS & SCRIPTING ATTRIBUTE

You notice that some of our scripts are getting TO LOOOONNG. To better organize the code let us regions.

C# REGIONS

Regions in C# is a useful feature that helps manage code in areas that can be hidden or visible.

Regions are created in this format:

#region NameOfTheRegion

your code here

#endregion

EXAMPLE👇

Where going to start a #region where the first power up method starts and a #endregion where the last power up method is

When you add it you will notice that a minus symbol appear next to the #region, press it and see how everything is tuck away.

HEADER ATTRIBUTE

Your Inspector is looking like this, using the [Header(“”)] will allowed us to distinguish a section per variables that are either public or have the [SerializeField]

I moved my _lifes variable to be up with the _speed variable

Lets do the SpawnManager

TOOLTIP ATTRIBUTE

This will allowed us to place comments that only appear if your mouse curser in on top of the public variable or has the [SerializeField]

Next up will be to update our Input to use Unity’s New Input System😆

🎮PART 9🎮

--

--

Rehtse Studio

Game Developer sharing content around Unity, programing and tips and tricks in game dev.