enter image description here

We're making a game that plays with a LineRenderer, and we want to give it a different visual feedback relative to the side of the screen that it's on.

We want to change the feedback of the specific line segments that are on the other side (when they past the middle point).

The API lets you map the segments positions, but isn't there a way that you can change the material/color of that specific segment? I just see that you can do it, but only for the whole Line Renderer, not segment-by-segment.

  • \$\begingroup\$ No, the whole LineRenderer is drawn in a single draw call, with the same materials applied to the whole thing. To use different materials on different segments, you'd need to manually break it into multiple LineRenderers. However, you can use a shader in a single material to modify the appearance based on the world position or screen position of each fragment (pixel) being drawn. Is the red zig-zag texture there representative of the change in appearance you want? If not, can you please include more details about how the appearance should change when it crosses the middle line? \$\endgroup\$
    – DMGregory
    Jan 29, 2017 at 18:58
  • \$\begingroup\$ That zigzag red line was just a vague representation of the segments that change their appearance when they cross the middle line. The appearance is a game design decision, that was not yet being made, but they want a different feedback relative to the side of the screen the segments are. So, using shaders you say? \$\endgroup\$ Jan 29, 2017 at 19:05

1 Answer 1


Here's an example of what you can do with a single material and a shader that changes behaviour based on world position:

Example of a squiggly LineRenderer changing colour as it dips below a horizontal line

This shader looks something like this:

Shader "Unlit/WorldspaceSwitch"
        _Color ("Surface Color", Color) = (1, 1, 1, 1)
        _UnderColor ("Underground Color", Color) = (0, 0, 0, 0)
        Tags { "RenderType"="Opaque" }
        LOD 100

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;

            struct v2f
                float3 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;

            // Declare the variables to hold the colours
            // we want to show above & below the line.
            fixed4 _Color;
            fixed4 _UnderColor;

            v2f vert (appdata v)
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);

                // I added a third texture coordinate,
                // and populated it with the worldspace y position.
                o.uv.z = mul(unity_ObjectToWorld, v.vertex).y;

                return o;

            fixed4 frag (v2f i) : SV_Target
                // This turns the worldspace y position
                // into a tight blending ramp from 0 to 1.
                float blend = saturate(i.uv.z * 10.0f);

                // This blends between the two colours.
                fixed4 col = lerp(_UnderColor, _Color, blend);

                return col;

You can use the same principle to make arbitrary changes to the LineRenderer's appearance, without resorting to multiple materials. If you want help creating shaders to achieve a specific effect, please feel free to ask a new question with details of what you want to create and what you have so far.

  • \$\begingroup\$ I have used shaders on a basic level, and just in OpenGL and Processing, but I'll give it a try first, and if I struggle, I'll let you know. Thanks in advance. \$\endgroup\$ Jan 29, 2017 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .