I can't figure out why the line renderer is misbehaving. I'm trying to draw a basic rectangle and here's the result: The rectangle is drawn in a clock-wise direction using 5 points. The first 3 lines end up twisted but the last line is fine. What am I missing? Here's my code for the line renderer object: Code (csharp): / the options var startWidth = 0.05; var endWidth = 0.05; var aMaterial : Material; // these are set in start private var line : LineRenderer; private var point01 = Vector3(0,0,0); private var point02 = Vector3(0,0,0); private var point03 = Vector3(0,0,0); private var point04 = Vector3(0,0,0); private var point05 = Vector3(0,0,0); function Start () { line = this.gameObject.AddComponent(LineRenderer); line.SetWidth(startWidth, endWidth); line.SetVertexCount(5); line.material = aMaterial; line.renderer.enabled = true; } function Update () { line.SetPosition(0, point01); line.SetPosition(1, point02); line.SetPosition(2, point03); line.SetPosition(3, point04); line.SetPosition(4, point05); }
I hope that's a joke. What is the technical reason for the lines being drawn in that way? What sort of drawing was it intended for?
Not a joke. To quote the docs, "The Line Renderer does not render one pixel thin lines. It renders billboard lines that have width and can be textured. It uses the same algorithm for line rendering as the Trail Renderer." Also: "The lines may seem to rotate as you move the Camera. This is intentional." --Eric
Yeah, I was just reading that part, but that's still no excuse for multiple points not rendering properly. I'm not trying to get a 1 pixel line; I need billboards that connect in an expected manner. I supposed it's possible to make use of 4 separate line renderers, but then why does the line render object support more than 2 points?
TBH the LineRenderer could be smarter about this sort of thing without losing its functionality. Right now, to get the direction to offset the vertices relating to a particular point, it looks only at the line segment ahead of it. It could be improved by looking both behind and ahead of it and averaging those vectors, which would remove this sort of artifact. Maybe you could Wishlist it?
This issue has bothered me as well. I don't see why anyone would want it to behave the way it does now. Fixed this issue by creating one line for each "part" of a larger twisting line.
Check the settings. If the line renderer has a final or starting width of zero, it might look like that.
The settings have been checked and re-checked. The line width properties are definitely equal, which should result in a flat, non-tapering line. I've worked around the issue by creating 4 separate line renderers, one for each side of the rectangle, but in more complicated applications I think that wouldn't fly. I assume this is a bug then that we should petition to get resolved in future releases. I see the line renderer as being a valuable tool when it works properly.
We know the LineRenderer does not work perfectly for a lot of cases. If you consider it a priority that it should be improved, I would indeed suggest making a suggestion of it on the UserVoice Unity feedback forum: http://feedback.unity3d.com/ Rune
+1 on this... the way the line render behaves makes no sense at all. Where two segments connect you would expect them to connect smoothly, otherwise what is the point of allowing for multiple coordinates?
I wanted to drop a little love for you for posting the best way that I could find to actually script the linerenderer. Your script is simple, understandable and works. After a day or so drowning in incomplete documentation and missing script examples, much like a baby learning to swim in an underwater invisible kelp forest, your simple post was all I needed to complete my own attempts at linerendering and see results on screen. Thank you very much!
Just want to revive this, as its no less irrelevant all these years later. Vectrosity is cool, but what would be cooler is if the line renderer would actualy render properly. Been twisting my neck as much as the line renderer recently...
Annndd i fixed it! So its actualy this easy. So in every joint, i add 2 points, instead of 1, and then i set my 1st new point to: laser.SetPosition(laserSize-1, Vector3.MoveTowards(hitInfo.point,lastLaserPoint,0.1f)); And my seccond to: laser.SetPosition(laserSize-1, hitInfo.point); I hope you all are helped with this discover. Vector3.MoveTowards(hitInfo.point,lastLaserPoint,0.1f)); Is used to move the point 0.1 units backwards so that i have a " perfect" stroke. Basicaly by offsetting 0.1 units i create the space for the " twisted" part to reside. and at point 2 i start my new laser. So if this gives issues for you, you can add 3 points, effectively clearing the last lasers end, and the new lasers start. But for me 2 points where good enough., 3 gives an even more cleaner effect tho. -edit- heres the code: Code (CSharp): lastDirection = direction; if(Physics.Raycast(position, direction, out hitInfo)){ laserSize+=3; if(laserSizeOld!=laserSize){ laser.SetVertexCount(laserSize); } laser.SetPosition(laserSize-3, Vector3.MoveTowards(hitInfo.point,lastLaserPoint,0.01f)); laser.SetPosition(laserSize-2, hitInfo.point); laser.SetPosition(laserSize-1, hitInfo.point); //Visualy Flips/Negates the last point testObject.transform.position = hitInfo.point; lastLaserPoint = hitInfo.point; return true; }else{ laserSize++; if(laserSizeOld!=laserSize){ laser.SetVertexCount(laserSize); } laser.SetPosition(laserSize-1, direction * 100); return false; }
Patrick234 had the right idea. After reading their post, I think I got the basic concept; what you want to do is retrace the entire path of the line in reverse after drawing it. Here is a very simple depiction of me doing just that. Code (CSharp): _lineRenderer.SetPosition(0, pos_lineAnchor); _lineRenderer.SetPosition(1, pos_lineTarget1); //This is the final point in the line before retracing it backwards. _lineRenderer.SetPosition(2, pos_lineTarget2); //Now draw it in reverse to cover the tapers. _lineRenderer.SetPosition(3, pos_lineTarget1); _lineRenderer.SetPosition(4, pos_lineAnchor);
Since even now I was having problems trying to find a good line renderer, I documented my trials with the above discussions in this blog post.
I wanted to throw in my solution since this is a high result on Google (and since Patrick1234's post helped me so much). I did a write-up on /r/Unity2D here, and I posted the code here.
We've just made a plugin that renders lines not facing the camera. I think it might help: https://www.assetstore.unity3d.com/en/#!/content/45806
okay LineRenderer sucks! i did not try @patrick1234 way of doing it but rather i created a prefab with a LineRenderer on it with my material and scale settings. Then i created a script for the prefab that has a public function setpoints(start,end). then another GameObject (the one i should have had LineRenderer on) with a public prefab GameObject of my LineRenderer prefab. Then the script on the main GameObject has a list of points (transforms) that at start instantiates a prefab for each line i want and calls setpoints with the start and finish position......ugh what a pain. My problem was LineRenderer having more then 2 points would skew the texture from point 1-2 but 2-3 was okay. or if i had 5 points the last point 4-5 would be good but everything else skewed a little. why cant scale start and end be for each segment? and why not multiple LineRenderers on an object! What is it supposed to do when you set start and end scales? Anyways, an hour later it looks okay. but not the way it should have been implemented... p.s. see game shots of my new game on instagram @birdmaze or birdmaze dot com
Hi all.. a very old thread, but I wanted to let you know that we are investigating ways to improve the issues raised here about the Line and Trail Renderers.
In case you haven't seen it - the 5.5 Beta contains an upgraded Line and Trail Renderer that should solve these problems. It's still in Beta, so we're still fixing a few issues, but hopefully you like it!
In order to fix this line width issue, I calculate a small new vector with the direction of the previous line and add it to the LineRenderer before adding the new point! Example of vector calculation: Code (CSharp): points.Add (lastHitPoint + Vector3.Scale(Vector3.Normalize(lastHitPoint - secondLastPoint),new Vector3(0.01f,0.01f,0.01f))); points.Add (newHitPoint); line.SetVertexCount (points.Count); for (var j = 0; j < points.Count; j++) { line.SetPosition (j, points [j]); }
Looks good! FYI, it is *much* faster to use LineRenderer.SetPositions instead of setting each position individually in a loop.
Just sitting here with the same problem: https://images-ext-2.discordapp.net...zZkLWdpZi5naWYifQ.oKMTednNR8DQ8Ukk6J4kyHUROAM Gonna test it on the 5.5 build soon
If someone ends here with the same problem, an upgrade to 5.5 with the new line renderer automatically fixed the problem: https://gyazo.com/0be2c64233ff045e00fb99cabfa936f5
Hi, just noticed this issue (Line twisting) on 2017.3.0b6 - did the fix for LineRenderer make it into this release? Is there an option I need to enable?
I know it's not exactly the place to ask this question but it's the closest post I found to my issue: I declared a Behaviour type variable just so I can enable and disable whatever component I assign to the variable, the thing is, I'm not able to assign lone renderer component to it, isn't line renderer's base class behaviour? Why can't I assign it as a behaviour type?
After much frustration, I came up with this solution for drawing local alignment circles. I really would like to see a fix for this. My script requires two LineRenderers, which isn't quite ideal but whatever. Code (CSharp): public class RangeDrawer : MonoBehaviour { public LineRenderer line1; public LineRenderer line2; /// Draw a circle on the X/Z plane. public void DrawRange( float radius, Vector3 center, int vertCount = 24 ) { line1.transform.position = center; //For use when dealing with Local space line rendering - remove if needed line2.transform.position = center; List<Vector3> pts1 = new List<Vector3>(); List<Vector3> pts2 = new List<Vector3>(); float angle = 90f; //For whatever reason, using 90 prevents jaggy issues. float stepSize = 360f / vertCount; for (int i = 0; i < (vertCount / 2); i++) { Vector3 pt1 = new Vector3( Mathf.Sin(Mathf.Deg2Rad * (angle)) * radius, 0f, Mathf.Cos(Mathf.Deg2Rad * (angle)) * radius ); pts1.Add( pt1 ); Vector3 pt2 = new Vector3( Mathf.Sin(Mathf.Deg2Rad * (angle + 180f)) * radius, 0f, Mathf.Cos(Mathf.Deg2Rad * (angle + 180f)) * radius ); pts2.Add( pt2 ); angle += stepSize; } pts1.Add( pts2[0] ); //Join our half circles and finalize. line1.positionCount = pts1.Count; line1.SetPositions( pts1.ToArray() ); pts2.Add( pts1[0] ); //Join our half circles and finalize. line2.positionCount = pts2.Count; line2.SetPositions( pts2.ToArray() ); } public void Clear() { line1.positionCount = 0; line2.positionCount = 0; } } .
Hey man I wish to Thank you for this amazing code. I making a coloring mechanic and when you drag the mouse around at start a line renderer is created and when you move it around, points are placed and then the line renderer adds that point to it, the thing was that when you changed direction of movement 180 degress the line would act wacky. And this solved it. This is the code I used maybe it clears the fog of how to use your idea in other situations for other people Code (CSharp): private void AddPointToLineRenderer(LineRenderer line, int index, Vector2 position) { int positionCount = line.positionCount; if (positionCount == 0) { line.positionCount++; line.SetPosition(index, position); currentPointIndex++; } else if(positionCount > 0) { line.positionCount += 4; positionCount = line.positionCount; line.SetPosition(positionCount - 4, Vector3.MoveTowards(position, line.GetPosition(positionCount - 5), 0.01f)); line.SetPosition(positionCount - 3, position); line.SetPosition(positionCount - 2, position); line.SetPosition(positionCount - 1, position); } }