- Home /
2D Dotted LineRenderer
Hi I have looked everywhere and couldn't find a way to draw a dotted line, from one vector2 to another. I can achieve this with just a solid line but it wasn't really the effect I was looking for.
(my thoughts where with a material which tiles the longer the line gets)
It would be preferable to use the LineRenderer but if that does have that sort of capability I am open to suggestions.
Thank you.
Also this line has to be able to move so a material which cant be changed wouldn't work.
Answer by CaptainKirby · Sep 05, 2015 at 02:37 PM
you got the right idea. 1 Make a dot on a texture, that is precicely in the middle of the texture. this will be used to tile on top of the line renderer. Make the line renderer and give it a material which has the dot on it(with transparency etc). Then attach a script to the same, and put in this=
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LineRendererDottedLIne : MonoBehaviour {
private LineRenderer lR;
public List<Vector2> positions;
private Renderer rend;
// Use this for initialization
void Start () {
lR = GetComponent<LineRenderer>();
rend = GetComponent<Renderer>();
}
// Update is called once per frame
void Update () {
for(int p = 0; p < positions.Count;p++)
{
lR.SetPosition(p, positions[p]); //sets the
}
rend.material.mainTextureScale = new Vector2((int)Vector2.Distance(positions[0], positions[1]), 1);
}
}
This is very rough, and it require that the positions List HAS 2 vectors AND ONLY 2. but you can build onto this i am sure :)
Could I see an example of the material that would work in this situation @Captain$$anonymous$$irby
and data if possible thanks, cant seem to get it to work
I am also trying to make a dotted line using LineRenderer. Can you post some code and the way you got it working?
Answer by vovo801 · Feb 13, 2020 at 04:39 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612002729im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can an UI Element know on which 3D Object it is pointing? 1 Answer
Enable prefabs in different scene on level load 2 Answers
How do i make a BattleLog? 0 Answers
Multiple NPCs 1 Answer
Scoring System Unity 6.4 C# 2D Game 1 Answer