- Home /
LineRenderer just showing awful results
Hi,
I'm trying to draw the outline of 2d colliders in gameplay. As far as I know the only way to draw a line on Unity Free is to use linerenderer or to buy some linedrawer from the asset store. LineRenderer gives awkward results because its meant to be 3D, here is what it looks like when I try to draw the outline of a BoxCollider2D:
Is there any way how I can achieve this in unity free? I also should be able to draw the outlines of a circle and polygon, but it's okay when I can implement these 2 later, I don't need them right now.
Seems to work fine for me
It seems you are setting the start width and end with to different values. If you are using inspector check these value. If you are using script then make sure you use lineRenderer.SetWidth(c, c) so both start and end width are the same.
Hm I have the same width, that's the entire code maybe something else is wrong:
(Initialising)
_lineRenderer = UnityObject.AddComponent(); _lineRenderer.SetColors(Color.red, Color.red); _lineRenderer.SetWidth(0.05f, 0.05f); _lineRenderer.SetVertexCount(5); _lineRenderer.useWorldSpace = false;
(When xSize or ySize got changed)
_lineRenderer.SetPosition(0, new Vector3(0, 0, z));
_lineRenderer.SetPosition(1, new Vector3(xSize, 0, z));
_lineRenderer.SetPosition(2, new Vector3(xSize, -ySize, z));
_lineRenderer.SetPosition(3, new Vector3(0, -ySize, z));
_lineRenderer.SetPosition(4, new Vector3(0, 0, z));
Answer by maccabbe · Mar 09, 2015 at 06:34 PM
Line renderer was simply not meant to draw multiple lines if any angle was less than or equal to 90. Line renderer prioritizes keeping the width constant but it can't do a good job if the angle between two lines is less than or equal to 90 because it considers the width of the vertex. However you probably want the vertices like they would in a frame so you have a few options.
1) Use multiple line renderers. I did this in my comment.
2) Make your own mesh and use mesh renderer. For instance, you could use something like the following.
public class NewBehaviourScript : MonoBehaviour {
Mesh mesh;
void Start() {
MeshRenderer renderer=gameObject.AddComponent<MeshRenderer>();
renderer.material=new Material(Shader.Find("Diffuse"));
renderer.material.color=Color.red;
MeshFilter filter=gameObject.AddComponent<MeshFilter>();
mesh=new Mesh();
mesh.vertices=new Vector3[8];
mesh.triangles=new int[]{
0, 1, 4,
5, 4, 1,
1, 3, 5,
3, 7, 5,
3, 2, 6,
6, 7, 3,
2, 0, 4,
6, 2, 4,
};
mesh.uv=new Vector2[mesh.vertices.Length];
mesh.RecalculateNormals();
mesh.Optimize();
filter.mesh=mesh;
}
public float z=0;
public float xSize=1;
public float ySize=1;
float width=1f;
void Update() {
mesh.vertices=new Vector3[]{
new Vector3(-width, -width, z),
new Vector3(-width, width+ySize, z),
new Vector3(width+xSize, -width, z),
new Vector3(width+xSize, width+ySize, z),
new Vector3(width, width, z),
new Vector3(width, -width+ySize, z),
new Vector3(-width+xSize, width, z),
new Vector3(-width+xSize, -width+ySize, z),
};
mesh.RecalculateNormals();
}
}
Answer by endeef · Mar 05, 2019 at 04:13 PM
I had the same problem. Using widthMultiplier instead of start-/endWidth fixed it for me.
LineRend.startWidth = LineWidth;
LineRend.endWidth = LineWidth;
LineRend.widthMultiplier = LineWidth;
width$$anonymous$$ultiplier totally fixed it for me. I left startWidth and endWidth to 1 and set width multiplier as indicated.
Answer by nathanthesnooper · Oct 23, 2016 at 11:37 PM
1) Use multiple line renderers. I did this in my comment.
This worked for me, but anyone still having this problem should update to Unity 5.5.0xb5 or later, Unity seemed to have fixed this bug.