Here I will tell you my steps to create an interactive game object using mouse position and Shader Graph shader in Unity. It is a shader with moving stripes. Using a script the closer the mouse to object, the faster the stripes will move downwards.

I am an absolute beginner to shader graph but made my way to create this simple project. To be an initial source for my future implementations and maybe helping people on their projects I wrote this tutorial.

Starters

In the beginning, create your project with a render pipeline. I tried to add LWRP to my initial project, but couldn’t make it work. That’s why I created a new project. Mine is a simple project, so I chose Lightweight RP.

If Shader Graph not included in your project packages, Window -> Package Manager -> Install Shader Graph.

Creating The Shader

1 — First the shader needs to be created. Right-click to Project folder -> Create -> Shader -> PBR Graph.

2 — Double click and shader is opened in Shader Graph.

3 — Let’s add a base color to the object which can later be changed in the inspector. Click plus sign in shader’s window and select the color you want. Then drag the node to the graph. Connect it with the Albedo input of the master node. Now we have a base color.

4 — The shader needs a texture. From Project window, drag your texture scene. Add a Multiply node to graph and connect RGB output of the texture and the color to this node. Connect the output of Multiply node with Albedo input of the master node. A single stripe can be seen from the preview.

5 — To increase the count of the texture, we need tiling. Add a Tiling And Offset node to the graph, connect its output to UV input of Texture node. When Y value of its Tiling input increased the repetition of stripes increases. I gave a value of 14 you can increase it however much you want. Now, there are multiple stripes generated.

6 — The last step is to move the sprites. Let’s add another property to our shader to control its speed from a script. Click on the plus sign on shader window, select Vector1. Since its value will be changed from the script contrary to Color, giving it a readable reference is important. It is _Speed in our case. Create a Time node and drag and drop the Speed property node to the scene. Time is used to have a constantly moving texture, with speed its movement speed is controlled. To enable this correlation again a Multiply node is used. Connect Time node’s Time output and Speed node to Multiply node’s inputs. Textures are moved by their Offset value so the output of Multiply node goes to Offset input of Tiling And Offset node.

7 — To save the changes made in the shader window, click Save Asset.

8 — Here is a side by side property information from the inspector and Shader Graph. Simply, the references are how the value of shaders can be called from the script.