After gameObject rotation is set, it changes to a different value
I am setting the rotation of a game object in some part of the code, as follows:
gameObject.transform.rotation = q1
where q1 is a quaternion with the new rotation I want for this game object. Immediately after the rotation is set to q1, the game object transform appears to be set correctly. I confirm this eg. via Debug.Log before I exit that function in my code. In the next Update call for this game object, I obtain the rotation and it's not what I set it to, but it's set to another value - say q2. The game object does not have a rigidbody attached to it. I have debugged my code and it does not set the rotation to q2. Is it likely the change to q2 is normal unity behaviour - and if so, how can I find out about where the object transform is set to q2.
Answer by ElijahShadbolt · Nov 25, 2016 at 10:36 PM
I don't know much about Quaternions, but maybe the Transform.rotation property does something to q1 when you set it, like clamp its angles? If you want the exact value of your quaternion to stay the same, maybe you could have a private Quaternion
in your script, and set transform.rotation
every update.
Thanks for your answer. If I Debug.Log the transform immediately after I set the rotation to q1, it appears to be set correctly. The change to the value q2 appears after that function has exited, Unity has taken control and I recover the game object later in the Update function. So it doesn't seem to be doing any clamping, at least not on the spot. On top of that, there seems to be no reason for any clamping or similar. Having the rotation as a property in my script defies the point of the transform in GameObject, don't you think?
correct me if I'm wrong but since Quaternions don't exceed 180°, it might very well be that you read an adjusted version of your Quaternion.
https://docs.unity3d.com/$$anonymous$$anual/QuaternionAndEulerRotationsInUnity.html
Also, why do you need the Transform's rotation quaternion to exactly match what you set it to in the previous update? I can't think of a reason why it would be necessary, if both Quaternions do indeed represent the same rotation.
Your answer
![](https://koobas.hobune.stream/wayback/20220612101419im_/https://answers.unity.com/themes/thub/images/avi.jpg)