Search the forums, there are many ways to draw a line between two points in Unity and this has been answered in the last 24 hours (not to mention many times in the past).
Hi JohnnyA, i did try to search in the forum before asking the question, maybe i have missed out something. I manage to find something to draw the line. But however the code start at the middle of the scene and it does not stop drawing the line when the mouse is clicked.
var linePoints : Vector2[];
var lineColor = Color.white;
var lineDrawOn = false;
private var lineMaterial : Material;
function Awake ()
{
lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass {" +
" BindChannels { Bind \"Color\",color }" +
" Blend SrcAlpha OneMinusSrcAlpha" +
" ZWrite Off Cull Off Fog { Mode Off }" +
"} } }");
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
}
function OnPostRender ()
{
if (!lineDrawOn || linePoints.Length < 2) {return;}
var cam = camera;
var nearClip = cam.nearClipPlane+.01;
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
GL.Color(lineColor);
var end = linePoints.Length-1;
for (i = 0; i < end; i++) {
var v = cam.ViewportToWorldPoint(Vector3(linePoints[i].x, linePoints[i].y, nearClip));
GL.Vertex3(v.x, v.y, v.z);
v = cam.ViewportToWorldPoint(Vector3(linePoints[i+1].x, linePoints[i+1].y, nearClip));
GL.Vertex3(v.x, v.y, v.z);
}
GL.End();
}
@script RequireComponent(Camera)
function Start ()
{
linePoints = new Vector2[2];
linePoints[0] = Vector2(.5, .5);
lineDrawOn = true;
}
function Update ()
{
var m = Input.mousePosition;
linePoints[1] = Vector2(m.x/Screen.width, m.y/Screen.height);
}