0
\$\begingroup\$

I'm trying to set Gizmos.matrix which is a 4x4 by matrix such that the gizmos drawn match the rotation of the object. However, I'm not sure how to convert the transform.rotation quaternion to a Matrix4x4. The Unity documentation points to a function called Matrix4x4.Rotate(), but Unity 5.6 does not seem to recognize any such function under Matrix4x4

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You need to use the Matrix4x4.TRS function. It takes a position vector, rotation quat, and a scale vector and outputs a Matrix 4x4.

public static Matrix4x4 TRS(Vector3 pos, Quaternion q, Vector3 s);

to draw the gizmo in the scene, use

//Just as one color assignment applies to every gizmo, 
//You must be sure to assign the correct matrix
Gizmos.matrix = Matrix4x4.TRS(transform.position,transform.rotation,transform.localScale);
Gizmos.DrawWireCube(Vector3.zero,Vector3.one);

Or alternatively:

Gizmos.matrix = transform.localToWorldMatrix;

Link to unity docs: https://docs.unity3d.com/ScriptReference/Matrix4x4.TRS.html

\$\endgroup\$
3
  • \$\begingroup\$ How do I use it such that the position and scale remain unchanged? \$\endgroup\$
    – jeanluc
    Aug 18, 2017 at 21:06
  • \$\begingroup\$ I guess I can set s to Vector3.one, but what about pos? \$\endgroup\$
    – jeanluc
    Aug 18, 2017 at 21:06
  • \$\begingroup\$ I've revised my answer to include a use case. \$\endgroup\$
    – Zebraman
    Aug 18, 2017 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .