Adding shields to some enemies

Personally, I prefer my enemies to be like tie fighters (no shields), but I don’t always get what I want.

We’ll start by duplicating our PlayerShield prefab, removing its prefab connection, and renaming it to EnemyShield.

I played around with the settings a bit, turned up some transparency and changed the color.
I also needed to change the scale on the transform a bit.

Go ahead and create a prefab once you are happy with it.

Modifying our Enemy script

As usual, the first thing we to do is add some variables.

Let’s go ahead and assign our EnemyShield prefab game object to the Enemy script component on our Enemy prefab game object.

Then we need to update our DoNullChecks() method.

Next we need to add a method we can call when we activate our shields.

We’ll also keep track of when an enemy has shields so they can take 1 hit before being affected by damage.

We’ll also need a way to remove the shield.

We need to update our OnTriggerEnter2D() method with a check for _shieldActive so that the shield will have an actual in-game effect.

It’s important to move our //destroy laser game object code outside the if(_shieldActive) statement so that the laser that hit the shield doesn’t survive.

Last, we update our Start() method to generate a random number, check if it equals 1 specific number in the range, and if so give that enemy a shield.

We can control how rare this is by increasing or decreasing the range for randomNumber.

That’s it, go have some fun with it!




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

Recommended from Medium

Why can API-First improve an API?

Python Web Scraping

BoostUp Group, A Global Space Startup, released its App on Google Play Store

Quarkus — Firts steps, part #1. Create a eclipse project. RESTEasy API

Announcement: Prophets of Hexarchia NFTs & Duelist Award

SDL Diagram Software for Linux

Python FASTAPI-decorator-for RBAC(Role based access control)

Hackathon Training: Part 1

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
GameDev Dustin

GameDev Dustin

More from Medium

Creating a Homing Missile

Cleaning up the Homing Missile code

The right way to wrap your UI inside the safe area — Unity

Using Unity’s ProBuilder To Model A Sci-Fi Container