- Home /
How to make Line Renderer lines stay flat?
I've noticed that as I add more vertexes to a Line Renderer line the line twists and stops being a smooth line.
All lines are on the same z level, even if I remove the materials the lines still seem to twist.
I have no idea why it does this, or how to go about resolving it, any suggestions?
Answer by giulio-pierucci · Feb 10, 2015 at 07:33 PM
This seems a normal behaviour of line renderer.
You may use GLdraw or Graphic DrawMesh (manually creating mesh or gldraw at runtime)
GlDraw may have some occlusion problems, so you can create a dynamic mesh and draw that.
http://docs.unity3d.com/ScriptReference/Graphics.DrawMesh.html
Thanks for the reply. So I would essentially make a mesh for each line, or a single mesh that just zig-zags with the lines? I'll read up on Draw$$anonymous$$esh.
No!!! 1 $$anonymous$$esh -> 1 Draw (usually)
You have to set all line points on 1 mesh (vertices, triangles, indices, uv, color)
then draw 1 mesh
Like this example, that create a quad mesh (is a partial example from my project, many variables are missing, consider only the mesh creation part)
$$anonymous$$esh mesh = new $$anonymous$$esh();
mesh.vertices = GetVertices(targetCamera.transform, hs);
mesh.uv = new Vector2[] { FirstUV, new Vector2(FirstUV.x, LastUV.y), LastUV, new Vector2(LastUV.x, FirstUV.y) };
mesh.uv1 = mesh.uv;
mesh.uv2 = (override2uv) ? Enumerable.Repeat(SecondaryUv, 4).ToArray() : mesh.uv1;
mesh.triangles = new int[] { // Left bottom triangle
0, 1, 2,
// Right top triangle
2, 3, 0
};
mesh.colors32 = Enumerable.Repeat((check == CameraSeeHaloCheck.Fading) ? GetColor(haloLayer) : color, mesh.vertices.Length).ToArray();
return mesh;
[...]
private Vector3[] GetVertices(Transform targetCamera, Vector2 halfSize)
{
Vector3 position = (offset == 0) ? transform.position : transform.position + (transform.position - targetCamera.position).normalized * offset;
return new Vector3[]
{
position + targetCamera.right * -halfSize.x + targetCamera.up * -halfSize.y,
position + targetCamera.right * -halfSize.x + targetCamera.up * +halfSize.y,
position + targetCamera.right * +halfSize.x + targetCamera.up * +halfSize.y,
position + targetCamera.right * +halfSize.x + targetCamera.up * -halfSize.y
};
}
Answer by thatCamelCode · Aug 23, 2018 at 07:47 PM
I realize this post is old, but if anyone else was trolling around the internet for a solution to this, I also found one by playing around with the LineRenderer settings that doesn't involve all this scripting.
When I did this, I was running Unity 2017.3.1f1 making a 2D game. Just go to the Alignment dropdown on the Line Renderer's settings and choose Local. This will stop the line from rotating around to match positions in space, and therefore it should appear flat the whole time.
Are you saying LineRenderer2D? I'm using LineRenderer and the dropdown selection for Alignment only has View and Transform Z....
Your answer
Follow this Question
Related Questions
Making a arrow instead of Linerenderer 2 Answers
Multiple Cars not working 1 Answer
Can't get linerenderers to work... 0 Answers
Distribute terrain in zones 3 Answers
Is there a way to make a Dotted Curved line in Unity for UI? 0 Answers