Drawing Lines with LineRenderer

Unity’s LineRenderer component gives you the ability to draw lines with multiple segments. You can use this to draw anything line based, like a rope or a finger drawing. This tutorial is for intermediate users: I will expect you to know your way around Unity reasonably well and understand basic code without explanation. Please see my earlier tutorials if you’re a beginner.

What is a LineRenderer?

A LineRenderer component is a standard Unity component that draws a line (in one or more segments) based on the settings you provide. You can set several options, most importantly:
  • The line’s positions (each point along the line, which will be connected to each other to form the line)
  • The line’s starting and ending thickness (how wide the line is drawn at its beginning and at its end)
  • The material that determines how the line looks.

Adding a LineRenderer

To use a LineRenderer you can add one in the Inspector (confusingly, it’s under Effects, not Rendering), or you can add one in code: [code language=”csharp”] var line = gameObject.AddComponent<LineRenderer>(); [/code] Here’s a screenshot of a simple LineRenderer with some sample settings you can copy into your own project: I’ve manually filled in the positions and line thickness, but you can of course do this in code too. You can only have a single LineRenderer component attached to a transform. If you need several, you can create a child transform for each on the parent.

Coding a LineRenderer

To setup your line you’ll need to give it the appropriate settings. Here’s a basic method to setup a LineRenderer with the key properties: [code language=”csharp”] void SetupLine() { line.sortingLayerName = "OnTop"; line.sortingOrder = 5; line.SetVertexCount(2); line.SetPosition(0, startingPoint); line.SetPosition(1, middlePoint); line.SetPosition(2, endPoint); line.SetWidth(0.5f, 0.5f); line.useWorldSpace = true; line.material = LineMaterial; } [/code] Let’s go through that code line by line. [code language=”csharp”] line.sortingLayerName = "OnTop"; line.sortingOrder = 5; [/code] These lines sets the line’s sorting layer and order. In my testing I’ve found that you need to set this when the line is created, as changing it later doesn’t have any effect. Make sure you set the line how you need it in relation to other objects in your scene. [code language=”csharp”] line.SetVertexCount(3); line.SetPosition(0, startingPoint); line.SetPosition(1, middlePoint); line.SetPosition(2, endPoint); [/code] These lines set how many points make up the line, and then adds a position to each point forming the line’s shape. Here I’ve set a line to have three points, then set each to the value of a vector variable. Note that the vertex count is 1-based, but SetPosition is 0-based. [code language=”csharp”] line.SetWidth(0.5f, 0.5f);[/code] This line sets the line’s width at the start and end (i.e. at the first and last points on the line). If you set both to be the same, the line will have a uniform thickness, otherwise the line will taper towards the end that is thinnest. [code language=”csharp”] line.useWorldSpace = true;[/code] useWorldSpace determines if the positions you set are based on ‘world space’ or are relative to the line’s transform’s position. You’ll usually want to set this as true. [code] line.material = LineMaterial;[/code] This line sets the material used to draw the line. If you don’t set a material the line will be magenta by default.

LineRenderer Weirdness

No GetVertexCount or GetPosition

For some reason there is apparently no way to get the number of positions in a LineRenderer or to add a new position without keeping track of the line details yourself. So I always keep a List<Vector2> (for 2D lines) and update that list every time I need to change the line’s positions. Here’s a method to add a new point to extend an existing line: [code language=”csharp”] void AddLinePoint(Vector2 newPoint) {        storedLinePoints.Add(newPoint); // add the new point to our saved list of line points        line.SetVertexCount(storedLinePoints.Count); // set the line’s vertex count to how many points we now have, which will be 1 more than it is currently        line.SetPosition(storedPoints.Count-1, newPoint); // add newPoint as the last point on the line (count -1 because the SetPosition is 0-based and Count is 1-based)    } [/code] As you can see, we need to keep our own list of points in order to know how many points there are so we can add a new one. You can similarly remove a point from the line: [code language=”csharp”] void RemoveLastLinePoint() {        storedLinePoints.RemoveAll(storedLinePoints.Count - 1); // remove the last point from the line        line.SetVertexCount(storedLinePoints.Count); // set the line’s vertex count to how many points we now have, which will be 1 fewer than it is currently       } [/code] That method removes the most recently added point from the line.

Weird Tapering

Unfortunately, LineRenderers have weird behaviour when they are made to turn on sharp angles. Due to the way they are rendered you will get the appearance of tapered line segments even when you have set the thickness to be even. There is no way around this; it’s just how it works. You won’t see the problem if you have thin lines or avoid sharp angles. You can mitigate the problem by adding some extra points to your line, especially near the sharp turns.  

That’s It

That’s the basics of Unity’s LineRenderer! Experiment with different settings to get different line looks.

One thought on “Drawing Lines with LineRenderer

Leave a Reply

Your email address will not be published.