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.
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:
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”:
In Awake, store the references, initialize the UnitShieldObject, and activate or deactivate the shield as required:
Create the Activate and Deactivate functions:
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.