- Home /
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!
Answer by andyborrell · Mar 07, 2018 at 02:10 PM
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.
Thanks a lot for the answer. For my problem I´ve described above your answer is working perfectly.
Normally I don´t know in which direction the object rotates next. Do I have to make a distinction of cases for all rotations or is there any general solution without that?
I'm not sure I understand your question. Is your problem that you have a UI where you can tweak x,y,z rotation values (like in the Unity inspector) and you want the object to rotate in world space around whatever axis you're currently modifying?
The second one: I get any rotation from the interface and I like to rotate my object. Is it necessary to check in which way the rotation has changed or is there any general way?
Answer by Bunny83 · Mar 07, 2018 at 03:37 PM
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.
Thanks for your comment. I think I can´t clarify my problem: I get angles form the interface and my object should rotate in these position. I get absolute values (from the world coordinate system), but I explained it with relative rotations just to make sure you understand me. So in my example I get further angles: first: (0, 90, 0), then (45, 90, 0). Important is, that this is just one case. I also get other angles from other axis in other orders. Is there a general way to solve it?
Answer by T86 · Mar 08, 2018 at 08:47 AM
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 translate.Rotate(Vector3, space.world)
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?
Your answer
