Creating Spaceship Shield Strength Feature in Unity

Objective: Make Shield take up to 3 hits and change the color in every time it is damaged

The original shield consisted of a game object disabled and once the player picked the powerup we activated it until the player was damaged.

In order to create this feature I made the shield a prefab and deleted it from the player.

Then I created a variable in the player script to have the reference to the prefab and wrote a function to Instantiate a shield.

Then I added a line to check is the player already have a shield then destroy it and create a new one, next make the shield child of the player.

At this point we have a indestructible shield, now I have to continue with the damage feature for the shield.

I created a shield script and attached it to the prefab. Then in the script I created a variable for the lives of the shield and a function to subtract a live when the shield is damaged.

Now in the enemy script I check for the collision with the shield and called the Damage function.

In order to indicate the shield was damaged in a visual way I created a variable to get the sprite renderer and add conditions in the damage function to change the color of the shield depending on the remaining lives.

With this change we can see how the shield is damaged.

Ok everything looks good, but there is a bug. When the shield collides with an enemy laser it don't suffer any damage.

To fix this I added the function OnCollisionEnter2D in the shield script to check for the Enemy Laser and call the Damage function.

And that’s it!!! We have a completely functional shield strength feature in the game.

--

--

--

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

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

Recommended from Medium

Frameworks War

Linear Data Structures — Linked Lists

CSS Variables (Custom Property)

Database Required for building a real -time Analytics Platform — Part 2/5

Do You Know Why Local Variables Are Thread-safe in Java?

Google Summer of Code: Week 11

Exploring Power Laws with Neo4j

Supercharge your Jira Quick Filters

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
Rusben Guzman

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

More from Medium

Variables

Animating Sprites In Unity

Quick tip — Enabling post-processing in Unity Universal Render Pipeline

Pump The Brakes…