Why does assigning a quaternion to transform rotation work but calling transform.rotation. doesnt?
I have 2 simple lines of code, one works and one doesn't.
// works - changes rotation
playerGravityObj.transform.rotation = Quaternion.FromToRotation(playerGravityObj.transform.right, rawGravityVec);
// does nothing...even if I use random parameters
playerGravityObj.transform.rotation.SetFromToRotation(playerGravityObj.transform.right, rawGravityVec);
both lines are in Update function
Answer by Hellium · Apr 28, 2020 at 06:17 PM
transform.rotation
is a property. It returns a Quaternion
, most likely handled in the C++ side.
Because Quaternion
is a struct (a value type), the getter does not return the "real" rotation but a copy of the rotation handled in the C++ side.
So when calling playerGravityObj.transform.rotation.SetFromToRotation(playerGravityObj.transform.right, rawGravityVec);
you are calling the SetFromToRotation
on a copy of the quaternion returned by the getter, thus, there is no result.
However, when you call playerGravityObj.transform.rotation = Quaternion.FromToRotation(playerGravityObj.transform.right, rawGravityVec);
, the setter takes the input value and apply the rotation on the C++ side, and thus, the rotation takes effect.