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 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .