0

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?

Aditya Chandra
  • 157
  • 1
  • 3
  • 13

1 Answers1

1

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);