- Home /
How to use Euler angles to rotate an object ?
I have an object that I need to rotate from its local rotation by an amount of degrees,on all 3 axis? I have never used euler angles before, but I know that I should be using it as a normal rotation will not work.. Lets say I want to rotate my object with an angle of Vector3(264,29,319) and it's current localRotation is (6,331,41) and the wanted final rotation of the object is (270,0,0).
Answer by robertbu · May 23, 2013 at 10:26 PM
Here are my suggested "rules" for how you deal with eularAngles in this situation:
Never read eulerAngles. Consider them write-only.
Keep your own Vector3 that represents the current rotation.
Never assign to an individual eulerAngles component (like eulerAngles.y). Always assign a Vector3 to eulerAngles.
Here is a simple script:
#pragma strict
public var speed = 1.0;
private var v3To = Vector3(2025,0,0);
public var v3Current = Vector3(0,0,0);
function Start() {
transform.eulerAngles = v3Current;
}
function Update () {
v3Current = Vector3.Lerp(v3Current, v3To, Time.deltaTime * speed);
transform.eulerAngles = v3Current;
}
Note changes in rotation are made to v3Current and then v3Current is assigned to Transform.eulerAngles. This way v3Current will always reflect one euler angle representation of the 'physical' rotation. But note that the actual eulerAngles value will not necessarily match v3Current. But they will both represent the same 'physical' rotation.
Quaternion.Lerp() will select the shortest distance between two rotations, so that will not work for you. Vector3.Lerp() will literally interpret the values passed. So if you want to rotate the long way from 41 to 0, you have to change the values so the Lerp() walks that way. In this example, you would set the final rotation to 360 rather than 0.
Note in playing these games, you typically want to normalize the v3Current values back into a standard range (0 to 360 or perhaps -180 to 180) before calculating and setting the new destination angle.
Thanks, That is really helpful info. so what if I am trying to go from 290 to 270, will Vector3.lerp interpret it exactly the way I pass it?
Thanks!
Going from 290 to 270 it will go the short way. If you want to go the long way, you will need to use 630 (270+360=630) as the destination. As the end of the rotation, your Vector3 will say 630, but eulerAngles will likely say -90. As mentioned above, before you use that Vector3 again, you will want to normalize your angles into a set range (0 - 360 more likely).
This was very helpful. This is the first thing I've checked this forum for and it was exactly what I needed to know. Thanks!!!
That for elaborate explanation. I've took it's reference thrice today.
Answer by darthbator · May 23, 2013 at 05:55 PM
Why not use Quaternian.Lerp to LERP the rotation to the target?
Wouldn't the LERP take the shortest way possible to the final angle and in the case mentioned above, The current z angle is 41 and my final z angle is 0, which means it will decrement the angle by 41 ins$$anonymous$$d of incrementing it by 319 which is what I want ?
Answer by Zplintz · May 23, 2013 at 07:40 PM
I'm just a noob to Unity (and 3d gaming/modelling) but I think there are fundamental issues with using Euler maths (in general - not just Unity) to rotate objects if you rotate more than one axis at a time - I believe you should be using Quaternian maths instead.
The issue is the possibility of a Gimbal Lock which a case in certain orientation of rotations, you use a degree of freedom. You can search it on youtube, there are quite nice videos about it