Raise the Shields… Using Shader Graph and C#

Thomas Kesler
4 min readApr 3, 2021

--

So, now it is time to work on implementing the Shield Power Up and making it look stellar.

Previously, we used shader graph to get a good looking animated shield for our player’s ship. When they collect it, we don’t want it to snap on out of no where but to do a powering up ‘animation’. I put animation in quotes because we will not be using Unity’s Animator for this effect, not when we can get better access through the Shader with C#.

First let’s figure out how we want the Shield Power Up to behave. For now, we will set the hits it can take to 3. Later we can open that for the designer to change. We also want the Shield to be a “Have or Have Not” type of Power Up. This is to say, that the Shield will not get recharged by another Collectible Power Up until the current Shield is completely depleted and disabled.

Let’s take a look again in the Shader Graph to see how we can access the Shader settings for script.

The Blackboard lists all Variables used in the Shader

The Blackboard shows a list of all the variables used in a Shader and their types. The list can be freely arranged for your level of access and organization.

Graph Inspector gives you detailed access to Node and Graph Settings

In the Graph Inspector, We can gain detailed access to any of the Nodes in the Graph or the Graph itself. Here I have the ‘FresnelPower’ Variable Node opened up. We are interested in three settings for now; Name, Reference, and Exposed. Firstly, Exposed is a Bool that let’s us access this variable outside of Shader Graph. This means both Unity Inspector and Scripts. Name is how this variable is identified in the Inspector and how it is labeled on the Graph. Reference is how you get a handle on the variable in a script. Let’s take a look at how we do that.

Note if you can see Either the Blackboard or Graph Inspector check the Top-Right of the Shader Graph window

Here is a couple of examples of how we access the FresnelPower in our script. Once we have access to the Renderer, the Shader is accessed through the Material component of the Renderer. Here we can assign the value of ‘_power’ to a separate float to work with it using GetFloat(). We can also use SetFloat() to place a new value into the Variable inside the Shader. And notice how in both functions take the name ‘_power’ as a string. This is how any of the ‘Exposed’ variables are ‘Referenced’.

The Shield Power Up Routine

As we can see from this Coroutine, we start the Shield power up the same as the others. We turn the ‘Active’ Bool on and set the value of the Shield hits. And since we are using a Game Object for our Shield, we also set that Object to active. Then we set up a ‘power’ float and set it to 12 and use SetFloat() on the Shield Shader to make it appear nearly invisible.

In the first While(), We set a new target value for our shield power. And the way Fresnel Power, the lower the number the strong it is. If we drop below 0, we get a nice flare when it reaches that point. To change the Power value, We are decreasing the Power by the deltaTime times 15. We can switch that value to a Variable that the designers to adjust.

In the second While(), We back the power to our default value. Again, We have the value we are checking against that can (and should) be set to a variable that the Designer to adjust. This time we are adding deltaTime with a slower multiplier to get a Slow pull back from the flare.

As our last step in the Powering Up Sequence, we set the Power of the Fresnel to our default ‘Full’ Strength to compensate for any overflow that may have come from using deltaTime.

Next time, We will look at taking damage to the Shield and how we use the same principals to show that lose on the Shield itself.

--

--

Thomas Kesler

A Unity Developer with a fondness for Fantasy games and the challenge of pushing boundaries.