Unity How to Draw a line with LineRenderer

  • Attach this script:
DrawLine.js
var pos1 : Vector3;
var pos2 : Vector3;
 
function Start () {
	var lineRenderer : LineRenderer = gameObject.AddComponent.<LineRenderer>();
}
 
function Update () {
	if (Input.GetMouseButtonDown(0)) { 
		pos1 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
		pos1 = Camera.main.ScreenToWorldPoint(pos1); 
		pos2 = pos1;
	}
 
	if (Input.GetMouseButton(0)) {
		pos2 = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 0.5);
		pos2 = Camera.main.ScreenToWorldPoint(pos2); 
	}
 
	if (pos2 != pos1) {		
		var lineRenderer : LineRenderer = GetComponent.<LineRenderer>();
		lineRenderer.SetPosition(0, pos1);
		lineRenderer.SetPosition(1, pos2);
	}
}
Navigation
Print/export
Toolbox