Unity 3D Game Engine – Get Mouse – Draw Line
1. Main Camera attach the script LineMouse.js
LineMouse.js
#pragma strict // It automatically adds Add Component> Effects> Line Renderer @script RequireComponent(LineRenderer) var lineRenderer : LineRenderer; var myPoints : Vector3[]; function Start () { // Setup of LineRenderer Component lineRenderer = GetComponent(LineRenderer); lineRenderer.SetWidth(0.2,0.2); } function Update () { if(myPoints){ lineRenderer.SetVertexCount(myPoints.Length); for(var i = 0;i<myPoints.Length;i++){ lineRenderer.SetPosition(i,myPoints[i]); } } else lineRenderer.SetVertexCount(0); if(Input.GetMouseButtonDown(0)){ // it is only drawing 10 points per second, as per the line InvokeRepeating("AddPoint",.1,.1); } if(Input.GetMouseButtonUp(0)){ CancelInvoke(); myPoints = null; } } function AddPoint(){ Debug.Log("Add"); var tempPoints : Vector3[]; if(!myPoints) tempPoints = new Vector3[1]; else{ tempPoints = new Vector3[myPoints.Length+1]; for(var j = 0; j < myPoints.Length; j++) tempPoints[j] = myPoints[j]; } var tempPos : Vector3 = Input.mousePosition; tempPos.z = 10; tempPoints[j] = camera.ScreenToWorldPoint(tempPos); myPoints = new Vector3[tempPoints.Length]; for(j=0; j< myPoints.Length; j++) myPoints[j] = tempPoints[j]; }
NOTICE: it seems to be a little sluggish.