How To Draw Line On Screen In Unity (C#)

Draw Line On Screen - Unity (C#)


This is an interesting post on How To Draw Lines on the Screen in Unity (C#) using the LineRenderer component. The Unity documentation here manages to describe it in a very clear way. However, there is no info on how to add this via script i.e., during the runtime. We thought it would be nice to come up with an article which described this.

Create a new C# script and attach it to the Main Camera. Open the script and paste the following lines of code:

using UnityEngine;
using System.Collections;

public class DrawLine : MonoBehaviour
{
 //reference to LineRenderer component
 private LineRenderer line; 
 //car to store mouse position on the screen
 private Vector3 mousePos;
 //assign a material to the Line Renderer in the Inspector
 public Material material;
 //number of lines drawn
 private int currLines = 0;

 void Update ()
 {
  //Create new Line on left mouse click(down)
  if(Input.GetMouseButtonDown(0))
  {
   //check if there is no line renderer created
   if(line == null){
    //create the line
    createLine();
   }
   //get the mouse position
   mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   //set the z co ordinate to 0 as we are only interested in the xy axes
   mousePos.z = 0;
   //set the start point and end point of the line renderer
   line.SetPosition(0,mousePos);
   line.SetPosition(1,mousePos);
  }
  //if line renderer exists and left mouse button is click exited (up)
  else if(Input.GetMouseButtonUp(0) && line)
  {
   mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   mousePos.z = 0;
   //set the end point of the line renderer to current mouse position
   line.SetPosition(1,mousePos);
   //set line as null once the line is created
   line = null;
   currLines++;
  }
  //if mouse button is held clicked and line exists
  else if(Input.GetMouseButton(0) && line)
  {
   mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   mousePos.z = 0;
   //set the end position as current position but dont set line as null as the mouse click is not exited
   line.SetPosition(1, mousePos);
  }
 }
 
 //method to create line
 private void createLine()
 {
  //create a new empty gameobject and line renderer component
  line = new GameObject("Line"+currLines).AddComponent<LineRenderer>();
  //assign the material to the line
  line.material =  material;
  //set the number of points to the line
  line.SetVertexCount(2);
  //set the width
  line.SetWidth(0.15f,0.15f);
  //render line to the world origin and not to the object's position
  line.useWorldSpace = true;  

 }
}


Save the script and return to Unity. You should notice that the script expects a material to be attached. Create a material if you don't have one already and add it to the script.
Play the scene and you should notice that there is nothing on the scene. Left click your mouse on the screen and move it while the button is clicked down, you should notice that the line is being drawn on the screen.

See you around.
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
Connect with him on:
    Blogger
    Facebook

8 comments:

  1. Excellent Post!!
    But i want to know how to restrict play to draw a single line.
    Thanks in advance!!

    ReplyDelete
  2. Hi, thank you for the script - this works well!
    Can I please have some advice on how to convert this script to draw non-straight lines like squiggly lines (again 2D) with many vertices?
    Thank youuu

    Thanks

    ReplyDelete
  3. thx you, sooooo helpful !
    but if I try to give sprite like background, the line can't show, it's always hide under the bckground..
    how can I make the line keep visible even if I add some object?

    thk you very much for your answer

    ReplyDelete
  4. We expect that the new LG V30 will be a super success when it launches at the upcoming global event. LG V30 Release Date

    ReplyDelete
  5. It is not working for me in unity 5.5 editor

    ReplyDelete
    Replies
    1. Finally it works for me as well. LOL .

      only change was:

      mousePos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,
      Input.mousePosition.y, Camera.main.nearClipPlane));

      Delete
  6. hello, can you tell me how can i destroy the line after a few seconds? an also i woud like to add to ciecles at the start and the end of the line thanks.

    ReplyDelete