- Home /

# LineRenderer creating triangles instead of lines

Hi all, I am having 2 problems drawing a line between four points.

my line is not drawing correctly (some parts appear as triangles)...

when i flip the parent object of the line renderer by multiplying the x value in the transform's localScale by -1, i get an odd result... the one part of the line that is working as i expected (the lower section) appears empty and i just get more triangles in the other sections, instead of getting the mirror image of the original line...

my code so far:

```
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public Transform point1;
public Transform point2;
public Transform point3;
public Transform point4;
private GameObject flashObject;
private LineRenderer flashLineRenderer;
public Color flashColour = Color.white;
void Start ()
{
flashObject = new GameObject("FlashObject");
//ADD A LINE RENDERER
flashLineRenderer = flashObject.AddComponent<LineRenderer>();
Material flashLineMaterial = new Material(Shader.Find("Unlit/Transparent"));
flashLineRenderer.material = flashLineMaterial;
flashLineRenderer.SetVertexCount(4);
flashLineRenderer.SetWidth(1f, 1f);
flashLineRenderer.SetPosition(0, point1.position);
flashLineRenderer.SetPosition(1, point2.position);
flashLineRenderer.SetPosition(2, point3.position);
flashLineRenderer.SetPosition(3, point4.position);
flashLineRenderer.useWorldSpace = false;
flashLineRenderer.SetColors(flashColour, flashColour);
flashObject.transform.parent = this.gameObject.transform;
}
}
```

it would be great to get a bit of insight into what is causing these problems.

Thanks in advance!

### People who like this

**Answer** by jbarbeau
·
Aug 25, 2014 at 10:09 PM

I'm having the same problem with LineRenderer. The first segment renders a triangle, unless I look at the other side. From that perspective it's a rectangle. The last segment is always a rectangle.

I've played with linewidth etc, Here's my code:

```
lineRenderer.SetVertexCount (3);
lineRenderer.SetPosition (0, new Vector3(0.0f,0.0f,0.0f));
lineRenderer.SetPosition (1, new Vector3(5.0f,5.0f,5.0f));
lineRenderer.SetPosition (2, new Vector3(7.0f,2.0f,6.0f));
lineRenderer.SetWidth(0.1f,0.1f);
```

Does anyone have an answer? Are there 2 sides to the line? It's a billboard that always faces you.

One side renders a triangle, the other side renders a rectangle look at pics the underside pic.

I think I figured it out. Theres a singularity that occurs in the billboard logic for LineRenderer. Look at LineRenderer4 from a high angle you see it.

Now linerenderer5 looks OK, from front or back actually... If you look at a segment from an angle nearly parallel to the linesegment, you can get a triangle.

For me, it shouldn't matter, I will be rendering golf ball trajectories with lots of small segments.

**Answer** by MaLlorente
·
Oct 17, 2016 at 09:17 AM

As you can read here. The problem is that a line render is a mesh and Unity optimize the mesh by reusing some vertex. The result is that ugly triangle shape.

You can solve this by

Creating more points to smooth the edges. Its not an elegant solution but its simple and works, at least for me. I wrote a function that do that:

Vector3[] Generate_Points(Vector3[] keyPoints, int segments=100){ Vector3[] Points = new Vector3[(keyPoints.Length - 1) * segments + keyPoints.Length]; for(int i = 1; i < keyPoints.Length;i++){ Points [(i - 1) * segments + i - 1] = new Vector3(keyPoints [i-1].x,keyPoints [i-1].y,0); for (int j = 1;j<=segments;j++){ float x = keyPoints [i - 1].x; float y = keyPoints [i - 1].y; float z = 0;//keyPoints [i - 1].z; float dx = (keyPoints [i].x - keyPoints [i - 1].x)/segments; float dy = (keyPoints [i].y - keyPoints [i - 1].y)/segments; Points [(i - 1) * segments + j + i - 1] = new Vector3 (x+dx*j,y+dy*j,z); } } Points [(keyPoints.Length - 1) * segments + keyPoints.Length - 1] = new Vector3(keyPoints [keyPoints.Length-1].x,keyPoints [keyPoints.Length-1].y,0); return Points; }

(Solution copied form here)Coding your own line renderer, and not make it so economical. You can do this by generating a dynamic mesh. The mesh will consist of a series of thin quads. For each line segment, you can compute the four corners of the quad by calculating the normal of the line and a specified line width:

Vector3 normal = Vector3.Cross(start, end); Vector3 side = Vector3.Cross(normal, end-start); side.Normalize(); Vector3 a = start + side * (lineWidth / 2); Vector3 b = start + side * (lineWidth / -2); Vector3 c = end + side * (lineWidth / 2); Vector3 d = end + side * (lineWidth / -2);

**Answer** by Zodiarc
·
Oct 17, 2016 at 09:20 AM

If you don't need the line to be blocky, you can use the Chaikin path smoothing algorithm http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html It will produce a nicely smooth curve.

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.