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.

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.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Fitness Blender Clone

Generate Postman collections using Node JS

Implementation of React Hooks ,Context API & Reducer All Together…

Awesome Dynamic Forms in React

A Second Thought On Recursion

How to use authentication in full stack web app built with ReactJS and HapiJS using JWT : Part 1

Tailwind Weekly #12: Aaaaand… we’re back 🌟!

5903c8b39527e51bbb79c80354158e71

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Taylor

Vincent Taylor

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

More from Medium

2.5D Platformer: Dev Progress Update

Using Reflection Probes in Unity3D and the URP

Adding colliders to a scene

Unity TileMap: Smooth Movement in a Top-Down Game