Draw Circle on Ground Unity

The unity line renderer, helps you render lines, circles, and other shapes using curves. Every bit well as some coordinate points in unity so that you can create any sort of curves. You tin draw color lines in 2d to help you with raycasts or just draw laser beams between 2 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 yous want to draw a raycast of where a bullet might shoot to in a outset person game.

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

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

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

We will also expect 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 nosotros tin 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 showtime I'1000 going to create a basic unity projection which we can experiment with.

Creating our unity 2d line renderer projection

Start off past opening unity hub. Create a new second projection called unity 2d line renderer. Like beneath:

unity line renderer first project

With a new blank project. Nosotros want to offset by simply setting upward some basic game objects. So that nosotros tin just go 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 bones unity add together line renderer by script using c#

Allow the states 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 together(new Vector3(10, 10));         l.startWidth = 1f;         l.endWidth = 1f;         l.SetPositions(pos.ToArray());         l.useWorldSpace = true;        

This code volition produce a line like this.

unity line renderer line example

Permit'due south at present try practice something a little more than complicated. Allow'south create a triangle with the renderer.

To do this nosotros need the start width to be 0 and the end width to be a larger number. Likewise let'south modify our positions.

LineRenderer 50 = gameObject.AddComponent();

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

So permit's only walk through this code quick. So we create ii positions nosotros also create start and terminate widths.

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

Permit's make a square line renderer.

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

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

Let's now look at how we can do something somewhat more advanced allow usa try create a circle. For this we going to apply a few bones mathematics functions to utilize 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

Then as yous can see you tin create a whole lot of different shapes using a line renderer. This can requite y'all a lot of freedom to generate these shapes on the wing at run fourth dimension in your games.

Permit us now expect at how we can color our line renderer.

Unity line renderer color changes

For demonstrating colour in our renderer we volition go dorsum to using a our square shape.

Let's create 1 solid color for our line to practice that add these 2 lines to your code.

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

You need to then hit run on your project. You will go this pinkish for now. I will prove yous how to fix information technology.

2d color line

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

unity 2d line renderer

Click on that piffling circle icon and assign a material.

You should now take this where you lot color is correct.

unity line renderer fix color

Permit's create a slope for our line renderer.

Change your code to accept these colors.

                      l.startColor = Color.crimson;     l.endColor = Colour.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 just need two colors. Let'due south look at using a multiple color gradient.

Alter your code to use this instead.

          Gradient chiliad = new Gradient();          float alpha = 1.0f;          thou.SetKeys(             new GradientColorKey[] { new GradientColorKey(Color.dark-green, 0.0f), new GradientColorKey(Color.blood-red, 0.5f), new GradientColorKey(Color.bluish, 1.0f) },             new GradientAlphaKey[] { new GradientAlphaKey(alpha,0.0f), new GradientAlphaKey(alpha, 0.5f), new GradientAlphaKey(alpha, ane.0f) }         );         50.colorGradient = g;        

And then how this code works is we first ascertain a new gradient object. Nosotros accept a blastoff of 1.0 so we have full opacity. And then we demand to add gradient color keys for each color we want in our gradient.

So our slope key has two values, one is the colour the other is a time value.

The time value determines how far our slope will stretch here are two examples of what the to a higher place code would expect like vs the same lawmaking with different time values.

unity line renderer multiple color gradient

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

          new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Colour.crimson, 0.2f), new GradientColorKey(Colour.blue, i.0f)        

So every bit y'all can see the alter to 0.2f on the red scale makes our green part of our gradient shorter, because our dark-green is from 0 to 0.2f.

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

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

Let's now await how nosotros can draw a line renderer betwixt two game objects.

Unity line renderer between two gameobjects

For this department of the tutorial we desire to become and create 2 cubes. We will then create a line renderer which will draw betwixt these two game objects. So become alee in your project and correct click in your hierarchy and create ii cubes.

unity line renderer between two game objects

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

unity line renderer 3d

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

unity line renderer 3d objects

Let us too add together a directional calorie-free so that our scene is more lit upwards.

Y'all should now take 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 photographic camera and change this settings.

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

Then if you hitting play on your scene you lot will now end up with your 2 cubes in your scene in 3d perspective view.

Permit us now modify our lawmaking so we tin can draw betwixt two game objects.

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

One time y'all have updated your lawmaking to this. Go ahead and elevate your two cubes into the game object slots like this.

unity line renderer 3d draw between two game objects cubes

Yous should now stop upward with something like this.

line renderer between two 3d game objects

Let's move our code into our update method now so we tin can see how we can morph our line renderer by moving our cubes effectually 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 in one case per frame     void Update()     {         List<Vector3> pos = new List<Vector3>();         pos.Add(go1.transform.position);         pos.Add(go2.transform.position);         50.startWidth = 0.1f;         50.endWidth = 0.1f;         fifty.SetPositions(pos.ToArray());         l.useWorldSpace = true;     } }                  

You lot should at present exist able to do this when you run your project.

move line renderer around unity

That pretty much sums up how y'all can describe a line renderer between 2 objects in unity as well as motion information technology effectually and change information technology.

Unity line renderer operation

Allow'due south talk nigh performance. Line renderers mostly doesn't take a very high performance cost, but make certain to non over use them.

Likewise when using them make sure to have a practiced clean up strategy or pooling or re use strategy so that you lot don't keep creating new renderers.

Using all-time practices for cleaning upwards objects is always advise in unity. And then make sure to write sound code which volition help you clean up afterward yourself.

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

Allow's look at something a little more than advanced. Let us endeavour 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 y'all tin can depict any type of object if you provide enough points to construct it. Line renderers are useful for trails for aiming.

How exercise yous depict a line in unity?

You by and large would utilise a line renderer to depict whatever lines in unity.

How do you make a laser in unity?

Depending on how basic you want to get, you tin can use a line renderer to draw your lazer beam across the co ordinates of your ray cast. If yous want something more outcome rich you may desire to use a particle effect.

Final words

Thanks for following this tutorial on line rendering in unity second and 3d. If yous liked this tutorial delight share it on social media and consider subscribing to my youtube channel here: https://world wide web.youtube.com/channel/UC1i4hf14VYxV14h6MsPX0Yw

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

crosstheighty.blogspot.com

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

0 Response to "Draw Circle on Ground Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel