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

HOW DID WE BUILD THE FIT MEAL WEBSITE CLONE?

Namespace, Scope , LEGB rule and global and non-local variable in python

A First Netlify Function in Golang

Chrome’s Experimental Image Lazy Loading Flag

What is Kanban for Marketing, Freelancers, Web design services?

RAII without deterministic destructors

Hide User from Address Lists (AD Connect)

Where Do I Start (learning to code)?

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

Creating A Modular Powerup System in Unity

Unity Interface Layout

2.5D Platformer — Improving the Ledge Grab #10

Level Design in Unity 3D Part 1: The Floor