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.

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.

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.

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

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

Unity game developer / C# Programmer / Gamer