# Dotted line with line renderer

So, how can I achieve a dotted line effect with the line renderer? I have a small 8x8 dot texture that I’ve applied with repeat wrap mode and my renderer shader is set to mobile alpha blended. However the texture looks warped and, if I set it to tile at a given rate, say, 100, it will repeat 100 times along an axis. My line needs to grow and shring, however, so how can I keep the dotting size consistent?

This one took me a little while to figure out, but I was searching for the same thing you were.

The trick is to set the material scale programatically on the X axis. You can access the materials programmatically through LineRenderer’s materials array and change the mainTextureScale. Here is an example of setting it based on the distance between positions 0 and 1:

``````void Update() {
var line = gameObject.GetComponent<LineRenderer>();
var distance = Vector3.Distance(start.transform.position, end.transform.position);
line.materials[0].mainTextureScale = new Vector3(distance, 1, 1);
}
``````

You will need to find a way to get your start/end positions. In my use case, I was calculating those programatically as well and just stored them when I calculated them so I had them available.

You can adjust how much to scale by dividing distance. In my case, the texture on the line is a 16x64 image, with 64 being on the long axis. So I divided distance by 4, since the long axis is 4 times the size of the short axis. Yours is 8x8 and square, so you can just use distance directly.

Both LineRenderer and TrailRenderer have a property TextureMode. Set it to Tile or RepeatPerSegment to achieve a dotted or dashed line.

Had to do the same thing in Unity 2019.3.
Even though the TextureMode field exists on the LineRenderer, I was not able to figure out the correct math for not distorting the dots. So, I went with TextureMode set to Stretch. On the texture that I used as the dot on the line, I set WrapMode to Repeat. Edited the texture in PS to add some padding to spread the dots apart from each other (Canvas Size in PS).
Created a material with this texture. Used the shader Mobile/Particles/AlphaBlended.

Here’s a script that I used on my dotted line prefab (attached to the same object that has the LineRenderer component):

``````using UnityEngine;

public class DottedLineRenderer : MonoBehaviour
{
public bool scaleInUpdate = false;
private LineRenderer lR;
private Renderer rend;

private void Start ()
{
ScaleMaterial();
enabled = scaleInUpdate;
}

public void ScaleMaterial()
{
lR = GetComponent<LineRenderer>();
rend = GetComponent<Renderer>();
rend.material.mainTextureScale =
new Vector2(Vector2.Distance(lR.GetPosition(0), lR.GetPosition(lR.positionCount - 1)) / lR.widthMultiplier,
1);
}

private void Update ()
{
rend.material.mainTextureScale =
new Vector2(Vector2.Distance(lR.GetPosition(0), lR.GetPosition(lR.positionCount - 1)) / lR.widthMultiplier,
1);
}
}
``````

Another issue that I’ve encountered - I wanted to fade the start and end of the line into transparency by setting the color gradient on the line. For some reason this did not work for me if the line had only 2 points and both ends were transparent, but it did work when the line had more than 2 points. Here’s the code that I used to resolve this and add more points to the line:

``````// line renderer with 2 points only does not handle transparency properly:
lineRenderer.positionCount = linePointsCount; // I used linePointsCount = 10
for (var i = 0; i < linePointsCount; i++)
{
lineRenderer.SetPosition(i,
Vector3.Lerp(fromPoint, toPoint, (float) i / (linePointsCount - 1)));
}

var dottedLine = lineObject.GetComponent<DottedLineRenderer>();
if(dottedLine != null) dottedLine.ScaleMaterial();
``````

I hope this helps someone.