The unity line renderer, helps you render lines, circles, and other shapes using curves. As well as some coordinate points in unity so that you can create any sort of curves. You can draw color lines in 2d to help you with raycasts or just draw laser beams between two points or objects.

This will be rendered on run time using a line renderer component in unity. Line renderers are useful for drawing paths in your games.

For example a application of line renderers are in the famous game Angry Birds.

Where a path is drawn by dragging on a mobile screen.

Line renderer can also help in targetting systems where you want to draw a raycast of where a bullet might shoot to in a first person game.

The unity line renderer component can also be used in building basic drawing apps using the unity engine.

All these applications make line renderers a really important thing to learn in unity.

In this tutorial I will be going over a few principles. Drawing a unity line renderer between a mouse / screen tap, dragging it and drawing it on your scene.

We will also look at drawing a line renderer between two game objects.

Dotted lines is also something people are quite interested in and I will look at how we can draw a unity 2d line renderer which can give us a dotted line.

Let’s get started on this unity 2d line renderer tutorial, so to start I’m going to create a basic unity project which we can experiment with.

Creating our unity 2d line renderer project

Start off by opening unity hub. Create a new 2d project called unity 2d line renderer. Like below:

With a new blank project. We want to start by just setting up some basic game objects. So that we can just get to the part of just drawing a basic line renderer.

Create a empty game object. Right click in the hierarchy and create a empty game object like below.

Next create a new c# script and call it unity line renderer test. Like below.

Creating a basic unity add line renderer by script using c#

Let us a construct a basic line renderer script.

LineRenderer l = gameObject.AddComponent<LineRenderer>(); List<Vector3> pos = new List<Vector3>(); pos.Add(new Vector3(0, 0)); pos.Add(new Vector3(10, 10)); l.startWidth = 1f; l.endWidth = 1f; l.SetPositions(pos.ToArray()); l.useWorldSpace = true;

This code will produce a line like this.

Let’s now try do something a little more complicated. Let’s create a triangle with the renderer.

To do this we need the start width to be 0 and the end width to be a larger number. Also let’s change our positions.

LineRenderer l = gameObject.AddComponent();

List<Vector3> pos = new List<Vector3>(); pos.Add(new Vector3(0, 5)); pos.Add(new Vector3(0, -10)); l.startWidth = 0f; l.endWidth = 15f; l.SetPositions(pos.ToArray()); l.useWorldSpace = true;

So let’s just walk through this code quick. So we create two positions we also create start and end widths.

Where the point which makes the top of the triangle being a width of just 0 and the bottom portion being 15.

Let’s make a square line renderer.

This should be similar to the triangle shape. Except we will make our 0 width 15 to match the bottom. So here is the code.

LineRenderer l = gameObject.AddComponent<LineRenderer>(); List<Vector3> pos = new List<Vector3>(); pos.Add(new Vector3(0, 5)); pos.Add(new Vector3(0, -10)); l.startWidth = 15f; l.endWidth = 15f; l.SetPositions(pos.ToArray()); l.useWorldSpace = true;

Let’s now look at how we can do something somewhat more advanced let us try create a circle. For this we going to use a few basic mathematics functions to use pi and draw points.

for (int i = 0; i < pointcounter; i++) { var radian = Mathf.Deg2Rad * (i * 360f / segments); points[i] = new Vector3(Mathf.Sin(radian) * radius, Mathf.Cos(radian) * radius,0); }

So as you can see you can create a whole lot of different shapes using a line renderer. This can give you a lot of freedom to generate these shapes on the fly at run time in your games.

Let us now look at how we can color our line renderer.

Unity line renderer color changes

For demonstrating color in our renderer we will go back to using a our square shape.

Let’s create one solid color for our line to do that add these 2 lines to your code.

l.startColor = Color.red; l.endColor = Color.red;

You need to then hit run on your project. You will get this pink for now. I will show you how to fix it.

To fix this you need to head over to the right while running your project.

Click on that little circle icon and assign a material.

You should now have this where you color is correct.

Let’s create a gradient for our line renderer.

Change your code to have these colors.

l.startColor = Color.red; l.endColor = Color.white;

Follow the same steps from before and you should now get a square that looks like this.

This is now all good and well if we just need two colors. Let’s look at using a multiple color gradient.

Change your code to use this instead.

Gradient g = new Gradient(); float alpha = 1.0f; g.SetKeys( new GradientColorKey[] { new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 0.5f), new GradientColorKey(Color.blue, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(alpha,0.0f), new GradientAlphaKey(alpha, 0.5f), new GradientAlphaKey(alpha, 1.0f) } ); l.colorGradient = g;

So how this code works is we first define a new gradient object. We have a alpha of 1.0 so we have full opacity. Then we need to add gradient color keys for each color we want in our gradient.

So our gradient key has two values, one is the color the other is a time value.

The time value determines how far our gradient will stretch here are two examples of what the above code would look like vs the same code with different time values.

The code that produces the above is this gradient color keys.

new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 0.2f), new GradientColorKey(Color.blue, 1.0f)

So as you can see the change to 0.2f on the red scale makes our green part of our gradient shorter, because our green is from 0 to 0.2f.

I think we have now covered a lot on gradients with line renderers and ways to color your line renderer.

Also if you wanted to adjust transparency of your line renderer you would be able to do it by changing that alpha value.

Let’s now look how we can draw a line renderer between two game objects.

Unity line renderer between two gameobjects

For this section of the tutorial we want to go and create two cubes. We will then create a line renderer which will draw between these two game objects. So go ahead in your project and right click in your hierarchy and create two cubes.

Once done click on this 2d button to switch into 3d view.

Then move your cubes so they are somewhat apart in your scene like the below screenshot.

Let us also add a directional light so that our scene is more lit up.

You should now have something like this.

Finally to get our camera into 3d view we need to click on our main camera and modify this settings.

To make your camera align to your view you need to click on align to view in the menu like this.

So if you hit play on your scene you will now end up with your two cubes in your scene in 3d perspective view.

Let us now modify our code so we can draw between two game objects.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class UnityLineRendererTest : MonoBehaviour { // Start is called before the first frame update public GameObject go1; public GameObject go2; void Start() { LineRenderer l = gameObject.AddComponent<LineRenderer>(); List<Vector3> pos = new List<Vector3>(); pos.Add(go1.transform.position); pos.Add(go2.transform.position); l.startWidth = 0.1f; l.endWidth = 0.1f; l.SetPositions(pos.ToArray()); l.useWorldSpace = true; } }

Once you have updated your code to this. Go ahead and drag your two cubes into the game object slots like this.

You should now end up with something like this.

Let’s move our code into our update method now so we can see how we can morph our line renderer by moving our cubes around the scene. So update your code to this.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class UnityLineRendererTest : MonoBehaviour { // Start is called before the first frame update public GameObject go1; public GameObject go2; LineRenderer l; void Start() { l = gameObject.AddComponent<LineRenderer>(); } // Update is called once per frame void Update() { List<Vector3> pos = new List<Vector3>(); pos.Add(go1.transform.position); pos.Add(go2.transform.position); l.startWidth = 0.1f; l.endWidth = 0.1f; l.SetPositions(pos.ToArray()); l.useWorldSpace = true; } }

You should now be able to do this when you run your project.

That pretty much sums up how you can draw a line renderer between two objects in unity as well as move it around and change it.

Unity line renderer performance

Let’s talk about performance. Line renderers generally doesn’t have a very high performance cost, but make sure to not over use them.

Also when using them make sure to have a good clean up strategy or pooling or re use strategy so that you don’t keep creating new renderers.

Using best practices for cleaning up objects is always advise in unity. So make sure to write sound code which will help you clean up after yourself.

We have now done some line renderers between two points, between two gameobjects, we have looked at different shapes and line renderer coloring.

Let’s look at something a little more advanced. Let us try and render a curve.

Frequently asked questions

What is line renderer in unity? A line renderer in unity is a object which allows you to draw lines. Using multiple points, so with a line renderer you can draw any type of object if you provide enough points to construct it. Line renderers are useful for trails for aiming.

How do you draw a line in unity? You generally would use a line renderer to draw any lines in unity.

How do you make a laser in unity? Depending on how basic you want to go, you can use a line renderer to draw your lazer beam across the co ordinates of your ray cast. If you want something more effect rich you may want to use a particle effect.

Final words

Thank you for following this tutorial on line rendering in unity 2d and 3d. If you liked this tutorial please share it on social media and consider subscribing to my youtube channel here: https://www.youtube.com/channel/UC1i4hf14VYxV14h6MsPX0Yw

I have also a bunch of other tutorials on game development in general. So feel free to check out some of those tutorials and articles from this list.