SteakOverflow SteakOverflow - 2 months ago 9
C# Question

Multiplication of a vector by a TRS matrix doesn't seem to work

Unity 3D 5.4 allows to create a Matrix4x4, set its TRS values via Matrix4x4.SetTRS and multiply a point by that matrix to apply the transformation. I would expect the following code to print out the translated point but the translation doesn't seem to take place.

m = Matrix4x4.identity;
m.SetTRS(new Vector3(-10f, 4f, 0f), Quaternion.Euler(new Vector3(0f, 0f, 0f)),;

Vector3 P = new Vector3(10f, 1f, 0f);

Debug.Log("P': " + m * P);

The code prints out

P': (10.0, 1.0, 0.0, 0.0)

instead of

P': (0.0, 5.0, 0.0, 0.0)

Am I missing something?


To transform a Vector3 point using a Matrix4x4, you need to use either Matrix4x4.MultiplyPoint() or Matrix4x4.MultiplyPoint3x4(). So in your case, I would try:

Debug.Log("P': " + m.MultiplyPoint3x4(P));

Also, as a tip - a Quaternion indicating no rotation can be represented by Quaternion.identity, so you don't need to keep calling Quaternion.Euler() for that (if that was your intention).

Hope this helps! Let me know if you have any questions.