Shield Strength

Yet Another Feature.

It was suggested that I should make the Shields be able to take more then 1 hit before the Player looses their Shields.

  • Visualize the strength of the shield. This can be done through UI onscreen or color changing of the shield.
  • Allow for 3 hits on the shield to accommodate visualization.

Code Changes

The Shields take Damage in the Damageable Behavior and are Activated on the Player on the Game Event Listener when the Shield Power Up is collected. In order to get this started I need to give the Shields a lives system, this is just a duplicate of the Lives already on the Damageable. I am going to convert this to a Stat. I am using float for my stat as that is what most games use for stat values. My Game uses int i.e. Lives and Ammo Amount, but I can display a float value as an int.

I also Created A Stat Scriptable Object Variable, A Stat Reference Variable, and a Property Drawer for the Stat Refernce.

Next I updated the Damageable Behavior to use the Stat Reference, replacing the lives and shield code.

I deleted the Player Lives Variable and created a new Player Lives Stat Variable. I also created the Shield Health Stat Variable. I then assigned all the values and updated the Damageable Component on the Player Prefab

Error! Error! Error!

Doing it this way is going to create A couple of errors. This is because I changed the type of the Lives from IntReference to StatReference. The Damageable Component on the Player had this Parameter set to use the Player Lives Variable. I could have Saved myself some work by using an Int Reference in the Stat instead of doing it as a float and not had to make a bunch of code changes. I did it this way because I wanted to be able to use all of my Scrpitable Object code in any future project and I wanted to make sure that all code in my project got updated to using the new Stat Variable.

Display Player Lives

It seems that I have created another problem.

The UI Display used the Player Lives Int Reference. That no longer exists it is now a Stat Variable. I have to change the Display Player Lives to use the new Stat instead.

The Display now works.

Player Behavior

Now I am getting an error on the Player Behavior, the last error. Again this Behavior use The Player Lives Int Variable which no longer exists.

I changed the Int Reference to a Stat Reference and added a local int value. In the Received Damage Method I added set the local int to the lives value. I then changed the if statement to a switch statement, using the private int lives instead of the value of the Lives

I then made sure to set the stat variable on the Player Component of the Player Prefab.

Now when the Game runs and the Player Damaged Event is raised I get no errors.

Shield Visualization UI

Slider

Sliders are great for displaying changing Values in the UI. It has a MIN and Max Value and as you change the value the slider will fill.

I created A Slider called Shield Health Slider. I set the Anchors, Pivot, And Position to the top left. I then moved the Slider to the right by 10 and down by 65. I changed the Width of the slider to 100. This puts it just under the Player Lives Display.

I created the Stat Slider Display behavior and added it to the Shield Health Slider. I also created the Player Shield Damaged Game Event and added it to the Shield Health Variable so the Game Event gets raised when The shield Takes Damage.

Stat Slider Display Behavior

I need the stat that I want to display and the Slider that needs to be updated. I created the Update Display so that the display will be updated when the value changes. I set both the max value and the value of the slider.

I also created a Game Event Listener to listen for the Game Event for when the stat is updated, for this I am doing the same thing I did for the player power ups collected Game Event Listeners (adding it in through code instead of requiring the Game Event Listener to be added to the game object.

The UI Updates when the Game Event is raised.

A Better Display

I Updated the Slider so it displays more as a actual health/progress bar.

I deleted the Handle Slider Area Game Object from the Slider. I then set the Slider Target Graphic to be the Fill Game Object. I changed the Fill Area Game Object Rect to be 0 from the left and 0 from the right.

To fix the last of the Display I changed the Width on the Fill to be 0.

Shield Graphic Visualization

I also want to edit the shield graphics color to give a slightly different visualization to the Player. Every Sprite Component in Unity has a Color Parameter with properties for r, g, b, a. These properties can be changed in the inspector with the Color Picker Tool or through code. When changing them through code they are a floating point value with a range from 0(No Color) to 1 (Full Color).

In order to do this I need A Shields Sprite Color Changer Behavior that I can add to the Player Shields Game Object. The Shields Sprite Color Changer requires that I have a Sprite Component. I also need the Shield Health Stat and The Game Event to Listen for, again I will be adding the listener in code.

What I want to change is the color on the Sprite Render so I had to change the Type from Sprite to Sprite Render, I realized this mistake when I was trying to access the Color Property and it was not showing under the Intelli Sense when typing the code.

I get the Shield Health Percentage. Then I set the Color of the sprite render to a new Color with the percentage I just calculated.

I edited the Player Shield Prefab and Set the Values of the Parameters.

The Results in Game.

Shield Activation

Game Design decision time, what happens when the player collects a shield power up? Currently the the Shield Game Object Gets Activated, this does not match the new functionality. I need to make it change the value of the Shield Health instead. I no longer need the Shield Game Object. I need the Shield Health Stat instead. In the Start Method I turn the Shields off by removing the what ever the max of the health from the shields. In the Activate Method I turn the shields on by resetting the shield stat.

Collecting the Power ups works along with taking damage.

--

--

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