Layered Defense: Adding Enemy Shields (Game Dev Day 45)

Jack Leavey
2 min readOct 6, 2021

--

Objective: Give the standard enemies a chance to spawn with a shield that blocks 1 hit.

This is a fairly simple implementation as the shield logic already exists for the player.

I start by adding a new bool as well as a gameobject. The gameobject will be linked to the shield graphic.

Two new variables.
Enemy Shield has been dragged in. I duplicated one of the player shields and renamed it.

I added a some logic in void Start to give the ships a roughly 50% chance to spawn with a shield.

Int X will be a random number between 1–99

I also set the _shield.SetActive to be linked to the _isShieldOn Variable. This means if one turns on, the other will as well.

With that, the shield will show up, but it has no functionality. Some logic has to be placed in the OnTriggerEnter section of the script.

I reused this same chunk of code for if a missile hits the shield as well.

Simply put the projectile is broken (always), and then if the shield was up, it turns the shield off. If there is no shield up when the next projectile hits, it does the default destroy enemy logic.

Some enemies get shields, and those shields block a single shot.

--

--

Jack Leavey
Jack Leavey

Written by Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

No responses yet