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 hidden or 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
- Favorites in PackageManager
- LudumDare59 : Signal
- Unity Editor: Tree Generator
- Leaf/Foliage Generator Tools (Runs in Browser)
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
Recent Comments
- on Mesh Exploder (sources)
- on Sprite Sheet Flip Book Shader
- on Sprite Sheet Flip Book Shader
- on [Asset Store] PolygonCollider2D Optimizer
- on Trajectory Test Scene 2.0
- on Vector3 maths for dummies!
- on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- on Using RenderDoc with Unity (graphics debugger)
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by













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?