Adding a Shield to our Enemy — 2D Galaxy Shooter in Unity!

Chris Hilton
3 min readSep 15, 2021


Objective: To give the teleport enemy a shield that it spawns with.

Let’s now add a new feature to our teleport enemy which is going to be a shield that allows for 2 hits before it is able to be destroyed. Combined with the avoid shot (teleport feature) this is going to be quite a tricky enemy to kill! Let’s get into it…

Let’s Add the Shield to the Enemy

Let’s add the shield sprite to the ‘Teleport_Enemy’ prefab as a child. From here, we need to change the color of the sprite to Red (255, 0, 0, 255) and set the Sorting Layer to Foreground (2). I am also going to add an Animator for our shield sprite and use the existing shield animation controller that we have setup for the player. Lastly, we need to add a circle collider 2D and make the proper adjustments to the size:

Let’s Add Some Code to “TeleportEnemyBehaviour” Script

Start with creating some variables for our _shieldHits and a trigger bool switch for _isShieldEnabled. Next, let’s cache a new Color for our shield which is going to drop the alpha after the first hit. Then, let’s get a reference to the SpriteRenderer so we can change the color. The last variable we need is a SerializeField GameObject so we can drag and drop the ‘Enemy_Shield’ game object into the Inspector.

Within Start() let’s find the Enemy_Shield game object and grab the SpriteRenderer component (also null checking). We are also going to set the _isShieldEnabled and _enemyShield.SetActive game object to true.

Within our existing OnTriggerEnter2D() method, let’s call a ShieldDamage() method (which we are about to create) when the shield collides with the ‘Laser’ or the ‘Player’. As for the ‘Mega Laser’ or the ‘Homing Missile’ I want this to still be able to destroy the enemy regardless of whether they have a shield.

Finally, let’s create our ShieldDamage() method which is going to be called when the above ‘Laser’ or ‘Playertags collide with the Teleport Enemy Shield:

Within this method, we want to check If the _isShieldEnabled is true first of all and then if it is, we are going to decrease total _shieldHits by 1 and also set the shields sprite renderer color to almost half alpha. We are also going to play the explosion sound.

Within this If check, we are going to create a nested If check to see if the _shieldHits get to 0 or below and if it does then we are going to set the _isShieldEnabled and _enemyShield.SetActive to false.

Finally, once the _shieldHits are 0 or below and the shields have been destroyed we can run the existing CollisionExplosion() method which will blow up the enemy.




Chris Hilton

Passionate Unity Game Developer with a growing interest in QA