Learn to code by making a 2D space shooter game.

Objective: Implement Shield Strength and Shield Hit Visualization.

Before doing this we need to understand we need to allow the player shield to take three-hit and visualize when it takes a hit.

The first step lets create an int variable called shield strength inside the player script and initialize it to 3.

In the next step, we need to decrease the shield strength by one each time get hit when isShield Active is true.

The third step we need to check is if the shield strength is equal to 2 we need to do nothing.

In the next step, we need to check if shield strength is equal to 1 we need to do nothing that means no damage to the player.

When shield strength is less than 1 we need to disable the shield and set shield strength to 0 to make sure it is zero.

Now we need to set shield strength to 3 again when the shield is activated again.

Let's see the result.

our shield is working

Now we need to add shield visual when we get hit, Let's create a coroutine and call it ShieldHitVisual.

to remove the error give the amount of time you need to wait after changing the color before returning the normal color

Now let's modify shield color when we get hit, we do this by creating a variable of SpriteRenderer called shield renderer.

Attach shield rendered of the shield in the inspector to the player script on the inspector, select player game object expand it to drag the sprite renderer to the field.

open another tab select shield lock the tab select the Player do like above instructions.

In the next step go to Shield hit visual method change the color to red wait .5 seconds return the color to white.

Last we need to start the coroutine when shied strength is equal to 2 and when shield strength is equal to 1.

Let us see the Final result.



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
Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development