Creating GL Lines

Id like to set up a function that draw lines exactly like the Debug.DrawLine() function works.

I just want to say DrawLine(point1, point2) and it will draw a line between point 1 and two.

I I tried to use GL Lines (I have Pro)

the line get generated when its called. Could someone tell me what Im doing wrong? (I really just copied this from the documentation because I dont fully understand how it works.

	public static void DrawGLLine(Vector3 P1, Vector3 P2)
{
    GL.PushMatrix();
    GL.LoadOrtho();
    GL.Begin(GL.LINES);
    GL.Color(Color.red);
    GL.Vertex(P1);
    GL.Vertex(P2);
    GL.End();
    GL.PopMatrix();		
}

Im then trying to call it in the part of the script that gets updated every frame

DrawGLLine(cam.transform.TransformPoint(v[0]),hit_point4.point);

You can’t call it in Update since the first thing that happens in the render step (which comes after Update) is to clear the framebuffer. If you want to show something above the rendered cameras you need to use one of the post-events (OnPostRender or OnGUI for example). Also keep in mind that LoadOrtho will load a totally different projectionmatrix than your camera is using. You should also set a modelview matrix to transform the world coordinates into camera coordinates or your line won’t appear at the desired world coordinates.

You can set the perspective matrix from your camera.

Next thing is that you need a material to render with. If you don’t set one it’s quite random what material is used.

http://unity3d.com/support/documentation/ScriptReference/GL.LINES.html

I’ve used GL.LINES a lot but i have no time at the moment to show a full example.

From the docs:

GL immediate drawing functions use whatever is the “current material” set up right now. The material controls how the rendering is done (blending, textures, etc.), so unless you explicitly set it to something before using GL draw functions, the material can happen to be anything. Also, if you call any other drawing commands from inside GL drawing code, they can set material to something else, so make sure it’s under control as well.

GL drawing commands execute immediately. That means if you call them in Update(), they will be executed before the camera is rendered (and the camera will most likely clear the screen, making the GL drawing not visible).

The usual place to call GL drawing is most often in OnPostRender() from a script attached to a camera

Edited to include all relevant details. Docs here.