how to draw a line in 3d

The unity line renderer, helps you render lines, circles, and other shapes using curves. Likewise equally some coordinate points in unity so that yous can create any sort of curves. You can depict color lines in 2d to help y'all with raycasts or but draw laser beams between two points or objects.

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

For instance 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 tin also help in targetting systems where yous want to draw a raycast of where a bullet might shoot to in a beginning person game.

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

All these applications make line renderers a actually important matter to learn in unity.

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

We will as well look at drawing a line renderer between ii game objects.

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

Let's go started on this unity 2d line renderer tutorial, so to commencement I'thousand going to create a basic unity projection which nosotros can experiment with.

Creating our unity 2d line renderer project

First off by opening unity hub. Create a new 2nd project called unity 2d line renderer. Like beneath:

unity line renderer first project

With a new blank project. Nosotros desire to start by just setting up some basic game objects. And so that we can just get to the part of just drawing a bones 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. Similar below.

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

Let united states of america a construct a basic line renderer script.

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

This code will produce a line similar this.

unity line renderer line example

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

To practise this we demand the outset width to exist 0 and the end width to be a larger number. Likewise let's change our positions.

LineRenderer l = gameObject.AddComponent();

                      List<Vector3> pos = new Listing<Vector3>();     pos.Add(new Vector3(0, 5));     pos.Add together(new Vector3(0, -10));     50.startWidth = 0f;     50.endWidth = 15f;     l.SetPositions(pos.ToArray());     50.useWorldSpace = truthful;        
unity line renderer triangle example

Then let's only walk through this code quick. Then we create ii positions we also create start and end widths.

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

Let's make a square line renderer.

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

                      LineRenderer 50 = gameObject.AddComponent<LineRenderer>();                  List<Vector3> pos = new Listing<Vector3>();         pos.Add together(new Vector3(0, 5));         pos.Add(new Vector3(0, -10));         fifty.startWidth = 15f;         l.endWidth = 15f;         fifty.SetPositions(pos.ToArray());         fifty.useWorldSpace = true;        
unity line renderer square example

Permit's now look at how we tin can do something somewhat more advanced let us try create a circle. For this we going to apply a few bones 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);         }        
unity line renderer circle

And so every bit you can run into you lot tin can create a whole lot of different shapes using a line renderer. This can requite yous a lot of freedom to generate these shapes on the fly at run fourth dimension in your games.

Let u.s. now wait at how we tin can colour our line renderer.

Unity line renderer color changes

For demonstrating color in our renderer nosotros will become back to using a our square shape.

Let'southward create one solid color for our line to practise that add together these 2 lines to your code.

                      l.startColor = Colour.red;   l.endColor = Color.cherry-red;        

You need to and so hit run on your project. You will go this pink for now. I will prove you how to fix it.

2d color line

To gear up this yous need to head over to the right while running your project.

unity 2d line renderer

Click on that little circle icon and assign a material.

Yous should now have this where you color is correct.

unity line renderer fix color

Let'southward create a slope for our line renderer.

Change your lawmaking to have these colors.

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

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

unity line renderer color gradient

This is now all good and well if we simply need 2 colors. Allow's look at using a multiple color slope.

Modify your lawmaking to use this instead.

          Slope m = new Gradient();          float alpha = ane.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 = grand;        

So how this code works is nosotros starting time define a new gradient object. Nosotros have a blastoff of 1.0 and so nosotros take total opacity. And then nosotros need to add slope color keys for each color nosotros want in our slope.

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

The fourth dimension value determines how far our slope will stretch hither are two examples of what the in a higher place lawmaking would look like vs the same code with different time values.

unity line renderer multiple color gradient

The code that produces the in a higher place is this gradient color keys.

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

So as you tin meet the modify to 0.2f on the red scale makes our dark-green part of our gradient shorter, because our light-green is from 0 to 0.2f.

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

Also if you lot wanted to suit transparency of your line renderer y'all would be able to do it by changing that blastoff value.

Let'southward now look how we can draw a line renderer between ii game objects.

Unity line renderer between two gameobjects

For this section of the tutorial we want to go and create ii cubes. We will and so create a line renderer which will depict between these two game objects. So get ahead in your project and right click in your bureaucracy and create 2 cubes.

unity line renderer between two game objects

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

unity line renderer 3d

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

unity line renderer 3d objects

Allow united states also add a directional light and then that our scene is more than lit upward.

You should now have something like this.

unity line renderer two gameobjects light - draw line between points

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

To brand your camera align to your view you need to click on align to view in the carte like this.

Then if you hit play on your scene you will at present end upwardly with your 2 cubes in your scene in 3d perspective view.

Let us now modify our code so nosotros tin can draw between ii game objects.

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

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

unity line renderer 3d draw between two game objects cubes

You should now end up with something similar this.

line renderer between two 3d game objects

Let'south move our code into our update method at present so we can see how nosotros can morph our line renderer by moving our cubes around the scene. So update your code to this.

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

Y'all should now be able to practise this when you run your projection.

move line renderer around unity

That pretty much sums up how you tin draw a line renderer betwixt two objects in unity as well as move it effectually and alter it.

Unity line renderer performance

Allow'southward talk about performance. Line renderers generally doesn't have a very high performance cost, only make sure to not over use them.

Likewise when using them brand certain to have a skilful 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. And so brand sure to write sound lawmaking which volition help you lot make clean up afterward yourself.

We have at present washed some line renderers betwixt two points, betwixt ii gameobjects, we have looked at different shapes and line renderer coloring.

Let'southward expect at something a little more than advanced. Let u.s.a. 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 depict any type of object if you provide enough points to construct it. Line renderers are useful for trails for aiming.

How practise y'all draw a line in unity?

Y'all generally would apply a line renderer to draw any lines in unity.

How do yous make a laser in unity?

Depending on how basic yous want to go, you can apply a line renderer to depict your lazer axle across the co ordinates of your ray bandage. If you lot desire something more effect rich yous may want to use a particle effect.

Final words

Give thanks 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 evolution in general. So feel complimentary to check out some of those tutorials and articles from this list.

freemanthenecolasty43.blogspot.com

Source: https://generalistprogrammer.com/unity/unity-line-renderer-tutorial/

0 Response to "how to draw a line in 3d"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel