Look at this code please:
private void Test()
{
GameObject go = new GameObject();
Quaternion orientation = new Quaternion( 0, 0, 0.7071068f, 0.7071068f);
Matrix4x4 mat = Matrix4x4.TRS(Vector3.zero, orientation, Vector3.one);
Debug.Log("Matrix TRS");
Debug.Log(mat.GetRow(0));
Debug.Log(mat.GetRow(1));
Debug.Log(mat.GetRow(2));
go.transform.rotation = orientation;
Debug.Log("Game Object Rotation");
Debug.Log(go.transform.right);
Debug.Log(go.transform.up);
Debug.Log(go.transform.forward);
}
If you run it the result will be:
Matrix TRS
(0.0, -1.0, 0.0, 0.0)
(1.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 1.0, 0.0)
Game Object Rotation
(0.0, 1.0, 0.0)
(-1.0, 0.0, 0.0)
(0.0, 0.0, 1.0)
Yesterday I spent half a day on an algorithm I was sure to be mathematically correct because I could not achieve the correct result on the screen (I was expecting the TRS result instead I was getting the GameObject result).
I was trying to figure out what the error in my logic was, when then I realized that the problem is how the GameObject.Transform handles the rotation quaternion.
While the Matrix.TRS result should be identical to the one generated by a GameObject transform, they are instead different.
Or am I missing something?