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
- Create or Open an Empty scene
- Create a new Empty Game Object
- 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Related Posts
4 Comments + Add Comment
Leave a comment
Recent posts
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on
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?