I’m trying to draw thick lines with Gizmos.DrawCube.
So far i’ve managed to position the Cube within the line points, yet i’m stumped on how to rotate the gizmo in-place to fit the line points direction.
I know i’m going to have to use Gizmo.matrix, but it ends up offsetting the points.
All i want is the rotation on the center of the gizmo.

public static void GizmoDrawLine(Vector2 p1, Vector2 p2, float thickness, Transform t)
{
Gizmos.color = Color.red * 0.5f;
Vector2 direction = p2 - p1;
Gizmos.DrawCube(p1 + direction * 0.5f, new Vector3(direction.magnitude, thickness, 0f));
}
You could use something like the following (untested) method:
public static void DrawCube(Vector3 position, Quaternion rotation, Vector3 scale)
{
Matrix4x4 cubeTransform = Matrix4x4.TRS(position, rotation, scale);
Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
Gixmos.matrix *= cubeTransform;
Gizmos.DrawCube(Vector3.zero, Vector3.one);
Gizmos.matrix = oldGizmosMatrix;
}
idbrii
3
To add to VesuvianPrime’s great answer, since you have a Transform, you can use localToWorldMatrix to create your matrix. I think it’s the same as Matrix4x4.TRS(transform.position, transform.rotation, transform.lossyScale).
However, you can’t just plug that into your current GizmoDrawLine. But you could use it like this to make a frame around an object and follows its rotation:
public static void GizmoDrawLine(float gap, Transform t)
{
var line = 0.07f;
Gizmos.color = Color.red * 0.5f;
Gizmos.matrix *= t.localToWorldMatrix;
var height = new Vector3(line, line + gap * 2f, line);
var width = new Vector3(line + gap * 2f, line, line);
Gizmos.DrawCube(Vector3.right * gap, height);
Gizmos.DrawCube(-Vector3.right * gap, height);
Gizmos.DrawCube(Vector3.up * gap, width);
Gizmos.DrawCube(-Vector3.up * gap, width);
}