Gizmos.DrawLine - thickens?

Hey,
i am somehow having problems to see a Gizmos.DrawLine in my editor (for debugging) is there a way to let the line appear thicker?, so at the moment its just this:

Gizmos.color = new Color (0,1F,0,1F);

		if (lastCompletedVectorPath != null) {
			for (int i=0;i<lastCompletedVectorPath.Count-1;i++) {
				Gizmos.DrawLine (lastCompletedVectorPath*,lastCompletedVectorPath[i+1]);*
  •  	}*
    
  •  }*
    

Really old question here. In case anyone else needs to do something similar here is an easy way to do it.

var p1 = startPosition;
var p2 = endPosition;
var thickness = 3;
Handles.DrawBezier(p1,p2,p1,p2, Color.red,null,thickness);

Bunny83’s version didn’t work directly, but the idea is good, so I’ve changed it a bit to work in screen coordinates and now it works better:

public static void DrawLine(Vector3 p1, Vector3 p2, float width)
{
	int count = 1 + Mathf.CeilToInt(width); // how many lines are needed.
	if (count == 1)
	{
		Gizmos.DrawLine(p1, p2);
	}
	else
	{
		Camera c = Camera.current;
		if (c == null)
		{
			Debug.LogError("Camera.current is null");
			return;
		}
		var scp1 = c.WorldToScreenPoint(p1);
		var scp2 = c.WorldToScreenPoint(p2);

		Vector3 v1 = (scp2 - scp1).normalized; // line direction
		Vector3 n = Vector3.Cross(v1, Vector3.forward); // normal vector

		for (int i = 0; i < count; i++)
		{
			Vector3 o = 0.99f * n * width * ((float)i / (count - 1) - 0.5f);
			Vector3 origin = c.ScreenToWorldPoint(scp1 + o);
			Vector3 destiny = c.ScreenToWorldPoint(scp2 + o);
			Gizmos.DrawLine(origin, destiny);
		}
	}
}

Unfortunately Unity doesn’t allow to change the line width. So lines drawn are always one pixel lines.

You can either:

  • Draw multiple lines in parallel slightly offset.
  • Draw actual quads instead of lines. However this has to be done with the GL class manually.

For simple debugging it’s easier to draw multiple lines. You can create a helper method like this:

public static void DrawLine(Vector3 p1, Vector3 p2, float width)
{
    int count = Mathf.CeilToInt(width); // how many lines are needed.
    if(count ==1)
        Gizmos.DrawLine(p1,p2);
    else
    {
        Camera c = Camera.current;
        if (c == null)
        {
            Debug.LogError("Camera.current is null")
            return;
        }
        Vector3 v1 = (p2 - p1).normalized; // line direction
        Vector3 v2 = (c.position - p1).normalized; // direction to camera
        Vector3 n = Vector3.Cross(v1,v2); // normal vector
        for(int i = 0; i < count; i++)
        {
            Vector3 o = n* width ((float)i/(count-1) - 0.5f);
            Gizmos.DrawLine(p1+o,p2+o);
        }
    }
}

I haven’t tested this method as i don’t have a Unity installation at hand, but it should work.

Here is an easy way to draw thick lines in the scene view and on standard cameras in the game view. (Note that I think the ‘thickness’ is expressed in pixels).

    public static void DrawThickLine(Vector3 start, Vector3 end, float thickness)
    {
        Camera c = Camera.current;
        if (c == null) return;

        // Only draw on normal cameras
        if (c.clearFlags == CameraClearFlags.Depth || c.clearFlags == CameraClearFlags.Nothing)
        {
            return;
        }

        // Only draw the line when it is the closest thing to the camera
        // (Remove the Z-test code and other objects will not occlude the line.)
        var prevZTest = Handles.zTest;
        Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;

        Handles.color = Gizmos.color;
        Handles.DrawAAPolyLine(thickness * 10, new Vector3[] { start, end });

        Handles.zTest = prevZTest;
    }

For those who are trying to adjust circle thickness, here’s how. This example is using normals applicable to the 2D case:

Handles.color = Color.green;
Handles.DrawWireArc(transform.position, transform.forward, transform.right, 360, 0.2f, 2.0f);