Adding Shields To Enemy Ships!

In the process of creating new enemy types, I want to allow some enemies to spawn with a shield which can block a single otherwise-fatal hit.

Vincent Taylor
3 min readAug 12, 2021
One hit to cancel the shield, another hit to kill the enemy

Today’s Objective: Add the ability for enemies to spawn with 1-hit shields, and create a new enemy type focused on that.

Drag the existing enemy prefab into the scene:

Create (or copy from Player) the shield as a child object of the enemy ship

The Shield object needs a Sprite Renderer, a Rigidbody2D, and one of the 2D Collider components (E.g. Circle Collider 2D).

The Rigidbody2D is set to kinematic, with Gravity at 0. Rigidbody components count any child colliders as part of themselves. So in order to separate hits on the Shield from hits on the enemy, the Shield object needs it’s own Rigidbody2D component set to kinematic.

Create a new script called “UnitShield”:

Add these variables

In Awake, store the references, initialize the UnitShieldObject, and activate or deactivate the shield as required:

UnitShield’s Awake function

Create the Activate and Deactivate functions:

Activate and Deactivate
Create the ShieldHit function to handle collisions

Now create a new script called “UnitShieldObject”:

Add the UnitShieldObject onto the Shield GameObject as a component, and the UnitShield onto the Shield object’s parent, the enemy root object.

On the UnitShield component, set up the array for how many hits you want the shield to take, and what color the shield should be after each.

The Shield child object
Example 1: Shield stops 1 shot
Example 2: Shield stops 3 shots

All finished! Now the enemies have a functional shield which can take as many hits as I choose from the Inspector.

--

--

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.