0
\$\begingroup\$

I have a GameObject in Unity and I need to change its X and Z rotation without doing anything with the Y, as that would mess up everything.
Note that this sets the Y rotation to 0 and I don't want that (I want it to let it be be what it already is), so this isn't any good:

gameObject.transform.rotation = Quaternion.Euler(someNewAngle, 0f, someNewAngle);

What should I do?

\$\endgroup\$
2
1
\$\begingroup\$

Just take the previous value of the Y rotation using transform.rotation.eulerAngles.

So your code would look like this:

var yRotation = gameObject.transform.rotation.eulerAngles.y;
gameObject.transform.rotation = Quaternion.Euler(someNewAngle, yRotation, someNewAngle);
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.