3
\$\begingroup\$

I know, that this question was asked before, but I could not find a proper answer to this. So how can I make my line dotted? I added a texture with a dot, but it just gets stretched for the length of the line, which is obviously not what I want. I'm using C# btw, if some code is needed here.

Edit: Inspector:

enter image description here

Edit: New Code :

public class dashedLine : MonoBehaviour
{
    public Vector2 newPosition;
    private LineRenderer line;

    void Awake()
    {
        line = GetComponent<LineRenderer>();
    }

    void Update()
        {
        line.material.SetTextureOffset("_MainTex", new Vector2(Time.timeSinceLevelLoad * 4f, 0f));
        line.material.SetTextureScale("_MainTex", new Vector2(newPosition.magnitude, 1f));
        line.SetPosition(0, newPosition);
       }
}

This is what I get:

enter image description here

Edit: This works now, when I use a Texture which has multiple dots in a row and put it to a material with Shader: Unlit/Transparent. I still don't know, why it's not working with a single dot.

Edit2: I had to recreate the dot texture with a single dot and it worked, so there was some issue on my side. Everything works now fine.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Apply a texture with a circle to a material, assign that material to a line renderer, and attach a script to it that will scale the texture relative to the magnitude of the line. Here's some code:

public class LineScript : MonoBehaviour
{
    private LineRenderer line;

    void Awake()
    {
        line = GetComponent<LineRenderer>();
    }

    void Update()
    {
        Vector2 newPosition = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
        line.material.SetTextureOffset("_MainTex", new Vector2(Time.timeSinceLevelLoad * 4f, 0f));
        line.material.SetTextureScale("_MainTex", new Vector2(newPosition.magnitude, 1f));
        line.SetPosition(0, newPosition);
    }
}
\$\endgroup\$
8
  • \$\begingroup\$ Mhm I get an error, that he couldn't find my texture, my material is called Dotted_Line and the Texture, which I've put into "Albedo" is called Dot. I tried both and both didn't work. Any suggestions? \$\endgroup\$
    – TobiasW
    Mar 24, 2016 at 14:02
  • \$\begingroup\$ Try using a different shader - Unlit/Transparent, or Sprites/Default. \$\endgroup\$
    – Iggy
    Mar 24, 2016 at 14:04
  • \$\begingroup\$ There's nothing wrong with the material, but I guess Standard shader doesn't have a "_MainTex" properly. So using a different shader should fix it. Or download the source and look at what the textures are called in the default shader. My guess would be "_Albedo". \$\endgroup\$
    – Iggy
    Mar 24, 2016 at 14:10
  • \$\begingroup\$ Okay since I have some other setup with camera and stuff I changed the code a bit, so that I can manually set the new position stuff, but the line still just gets stretched and not dotted :/ Added my code in the question. \$\endgroup\$
    – TobiasW
    Mar 24, 2016 at 14:14
  • \$\begingroup\$ Okay I tried to update my question, so that I provide everything you could need to know. \$\endgroup\$
    – TobiasW
    Mar 24, 2016 at 14:25

You must log in to answer this question.

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