Aug
27
2017

Drawing Lines

Hi dear reader!

In this tutorial we want to learn how to draw a line in unity by user mouse movement and interaction.

First we need a component that would be able to render simple lines.
Our choice is ​Line Renderer​, this component is able to renderer lines between given positions so we can pass mouse position and let it to render them.

Getting Started

  1. Create or Open an Empty scene
  2. Create a new Empty Game Object
  3. Create a new c# script and name it ​DrawLine ​then put the below script into it

Now, attach the ​DrawLine​ script to Created Empty Game Object and Play the game.
Press primary mouse button and move your mouse around screen, as you can see the line goes to draw lines in your mouse position.

 

Explanation

In the script we first check the mouse primary button is pressed or not, when it is pressed we go to next step and try to get the mouse position in world space. After that we try to check if the mouse position isn’t exists in the line points, if the point doesn’t exists then we increase the line positions count and then we add mouse position to line positions and all done.

In the next tutorial we will try to use the line drawing in 2d mode and add a collider to the line.

 

Resources

This code mainly gathered from a gist and also is available in Unity Library

Thanks for reading.

 

Source Code


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLine : MonoBehaviour
{
[SerializeField]
protected LineRenderer m_LineRenderer;
[SerializeField]
protected Camera m_Camera;
protected List<Vector3> m_Points;
public virtual LineRenderer lineRenderer
{
get
{
return m_LineRenderer;
}
}
public virtual new Camera camera
{
get
{
return m_Camera;
}
}
public virtual List<Vector3> points
{
get
{
return m_Points;
}
}
protected virtual void Awake ()
{
if ( m_LineRenderer == null )
{
Debug.LogWarning ( "DrawLine: Line Renderer not assigned, Adding and Using default Line Renderer." );
CreateDefaultLineRenderer ();
}
if ( m_Camera == null )
{
Debug.LogWarning ( "DrawLine: Camera not assigned, Using Main Camera or Creating Camera if main not exists." );
CreateDefaultCamera ();
}
m_Points = new List<Vector3> ();
}
protected virtual void Update ()
{
if ( Input.GetMouseButtonDown ( 0 ) )
{
Reset ();
}
if ( Input.GetMouseButton ( 0 ) )
{
Vector3 mousePosition = m_Camera.ScreenToWorldPoint ( Input.mousePosition );
mousePosition.z = m_LineRenderer.transform.position.z;
if ( !m_Points.Contains ( mousePosition ) )
{
m_Points.Add ( mousePosition );
m_LineRenderer.positionCount = m_Points.Count;
m_LineRenderer.SetPosition ( m_LineRenderer.positionCount – 1, mousePosition );
}
}
}
protected virtual void Reset ()
{
if ( m_LineRenderer != null )
{
m_LineRenderer.positionCount = 0;
}
if ( m_Points != null )
{
m_Points.Clear ();
}
}
protected virtual void CreateDefaultLineRenderer ()
{
m_LineRenderer = gameObject.AddComponent<LineRenderer> ();
m_LineRenderer.positionCount = 0;
m_LineRenderer.material = new Material ( Shader.Find ( "Particles/Additive" ) );
m_LineRenderer.startColor = Color.white;
m_LineRenderer.endColor = Color.white;
m_LineRenderer.startWidth = 0.3f;
m_LineRenderer.endWidth = 0.3f;
m_LineRenderer.useWorldSpace = true;
}
protected virtual void CreateDefaultCamera ()
{
m_Camera = Camera.main;
if ( m_Camera == null )
{
m_Camera = gameObject.AddComponent<Camera> ();
}
m_Camera.orthographic = true;
}
}

view raw

DrawLine.cs

hosted with ❤ by GitHub


4 Comments + Add Comment

  • Is it possible to draw on perspective camera too?

    • Thanks for the question ❤️
      Yes, it is possible.

      • Minor Changes in input I achieved this for the perspective camera. Sincere thanks Buddy

  • Thanks for your response. Is it possible by changing the Camera.orthographic to perspective?

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.