Photo by Gioele Fazzeri on Unsplash

Day 47: Enemy Shields

Michael Little
4 min readFeb 27, 2023

--

I’m a little lost currently on Balanced Spawning in terms of getting my waves to pass in correctly, so I’m going to just move on for now then come back to it.

Current objective is to allow some enemies to have shields. This should be fairly simple as I’ve done multiple stories on my Player shield system. If I get lost here, I only need to go back to my stories for reference.

Alright, so I think enemy4 is going to have a shield so I will implement that later. I also want at least some original enemies if not all of them to have shields, so I’m going to pull original Enemy prefab into hierarchy.

We first need to look at the variables that have to do with Player shields and copy them to the first Enemy script.

We don’t need to focus on our variable type SpriteRenderer as that deals with the multiple shield colors for our Player and not even the shield strength actually as enemy shield only takes one hit currently. We just want the Enemy to have a shield then have it disabled when it receives one hit.

Our enemy shield will be dealt with a little differently because the Enemy only receives one hit before it is destroyed, so unlike the Player, we won’t be creating a ‘lives’ system.

Obviously need a method for the shields in enemy script:

Then we address our Enemy Shields process from where the Enemy collides with the Player and laser.

If you read it carefully and have a basic understanding of Csharp at this point, it should make sense. We obviously also need to address this where the laser collides with the enemy:

Alright, so just to save us a lot of hassle here, we’re going to create a separate new prefab for our Enemy shield visualizer that isn’t the Player shield visualizer, but it’s actually pretty simple, or should be:

Don’t forget to Apply all overrides on Enemy as well.

Now let’s see how that looks in Unity:

Unfortunately, it’s destroying enemy and shield in one hit, but I think this is because I forgot to add a ‘return’; where I turn the shields off:

What this does is prevent the Destroy enemy process while shields get turned off, or it should:

Boom, obviously I added a ‘return;’ in where enemy collides with the Player as well. We can edit shield color, what enemies get shields or randomize the possibility of enemy shields occurring later. The point is that the foundation is here. Achievement unlocked!

There’s also some unneccesary stuff as of right now like the Enemy shield method that I deleted, and I don’t think we even need a ‘isShieldsActive’ bool because there’s no conditions set for it. We can just turn the shield visualizer prefab off once it collides with player/laser, then a ‘return;’, but I’ll leave the bool for now in case it becomes of use later.

--

--

Michael Little

I'm currently pursuing my passion for game development. This is where I document education pertaining to that and software engineering. No personal Dev diaries.