c# how can I set the rotation of a GameObject to angles in world space

I get different angles from an interface and I like to turn a GameObject in order of these angles. The angles are given in world space.

As everyone can see here, the general code is: *transform.rotation = Quaternion.Euler(angles); *

But when I like to rotate the object

  • 90 degree around the y-axis

  • and then around the world-x-axis

the rotation in face is:

  • 90 degree around the y-axis

  • and then around the world-z-axis (= object-x-axis).

What am I doing wrong and

how can I rotate a gameobject in world space coordinates to any given angles?

Thanks a lot for your help!

The rotations performed by Quaternion.Euler are performed in the following order:

  • Z
  • X
  • Y

A simple way to perform rotations in any order you want is to combine multiple calls. So if you want to rotate around the Y axis, and then around the X axis you can make your quaternion like so:

Quaternion rotation = Quaternion.Euler(xRotation,0,0) * Quaternion.Euler(0,yRotation,0);

In multiplication the order of rotations are applied from right to left. So here the y rotation happens first, and then the x.

From your comments on the other answer i seems you are not interested to set the rotation to an absolute value but to perform a relative rotation. In this case you can simply do:

transform.rotation = Quaternion.AngleAxis(angle, rotationVector) * transform.rotation;

This will rotate the object relatively around the given rotationVector by “angle” degrees.

So this:

transform.rotation = Quaternion.AngleAxis(90, Vector3.up) * transform.rotation;

will rotate the object around the world up vector (yAxis) while this:

transform.rotation = Quaternion.AngleAxis(90, Vector3.right) * transform.rotation;

would rotate around the world xAxis. However if you want you can also pass “transform.right” or any other rotation axis you want to rotate around.

I´ve tried different options to turn my object in any angles in world space:

  • set the rotation to
    Quaternion.identity and turn it then
    in the quaternion order: Z,X,Y
  • set the rotation to Quaternion.identity and turn it with
  • rotate it like Bunny83 told in his answer

But with all the solutions I don´t get the correct rotation in world space of my object. I think the problem itself is very simple and a general one, I can´t belief that there is no standard solution.

Can anybody help?