- Home /
Euler Angles and Vectors
Hi, I am learning how to make a third-person game and I noticed that Euler Angles uses vectors in order to rotate objects. (Look at the marked area in the screenshot, mine is almost the same as that.) I am having trouble to understand how vectors can be used to represent the rotation of the object. Thank you in advance.
Answer by unity_ek98vnTRplGj8Q · Oct 16, 2020 at 04:39 PM
Any rotation can be represented by a sum of rotations about the 3 main axis (X, Y, and Z axis). For example if I wanted to represent a rotation about the y axis in the positive direction, or in other words a rotation to the right, I could use the vector (0, y, 0) where y represents how large of an angle in degrees I want to rotate. Similarly if I want to tilt forward or backward I could rotate about the x axis. A rotation of (0, 0, 0) represents an object that is aligned with the 3 axis ("forward" for the object is along the world Z axis, "up" for the object is along the world Y axis, and "right" for the object is along the world X axis). Therefore, the orientation of any object can be represented as an offset in degrees along some combination of the 3 axis, relative to this default orientation. So if I wanted to represent a more complex rotation, I could simply find which combination of basic axial rotations add up to represent the overall rotation.
It is worth noting that given any orientation there are multiple sets of Vector3's (what we call Euler Angles) that represent that orientation. For example, the default orientation can be represented by (0, 0, 0) OR (0, 360, 0) OR (360, 360, 360) etc.
Also worth noting is that it matters in what order you apply each axial rotation. When you give Unity a rotation of (10, 20, 30) it will first take the rotation about the X axis, then apply the rotation about the Y axis, then apply the rotation about the Z axis. Other programs may not use this convention. To see why the order matters, imagine a rotation of (90, 90, 0). Rotating about the X axis then the Y axis (IMPORTANT: this is the new local Y axis after applying the rotation, NOT the world Y axis) will give a different rotation than first rotating on the Y axis, then rotating on the X axis. Edit: The order is actually Z, then X, Then Y
I would also like to mention that using Vector3's (Euler Angles) to represent a rotation is great for small rotations or rotations about 1 or 2 axis, mostly because they are intuitive and easy for a person to look at and grasp the orientation of the object. There are several complications that using Euler Angles can cause for more complex rotations (gimbal lock), so Unity and every other 3D graphics program out there uses Quaternions to actually represent the object's rotation. Quaternion's are a more complicated but much more robust way of representing rotations. While you can use Euler Angles to manipulate rotations in Unity, Unity always first converts those Euler Angles to a Quaternion before doing anything with them. It is also super useful to learn how to use the Quaternion class in Unity. You don't need to know what a Quaternion is or how it works, but the helper functions in the Quaternion class are enough to do any rotational manipulation you want.
Unity's euler angles order is Y-X-Z or Z-X-Y depending if you think about local or worldspace rotations. So when you think of a non rotated object as a basis, the rotation represents Y degrees around the objects local y axis, followed by X degree on the local (now rotated) x axis, followed by Z degrees around the (now rotated) z axis.
Another way to view it is Z degrees around the global z axis, followed by X degrees around the global x axis followed by Y degrees around the global Y axis.
You said the order is X-Y-Z which is not true, see the documenation for reference.
In Unity these rotations are performed around the Z axis, the X axis, and the Y axis, in that order.
Thank you for the correction, I will edit my answer
Answer by elnurv47 · Oct 16, 2020 at 05:21 PM
Thanks for your reply. I still can't get this topic. Let's say we have a script like this:
As we know it makes the character move forward 1 unity if we press "W". Right ? The thing that I don't get is that how those x, y, z are converted when we write something like this:
target.eulerAngles = new Vector3(0, 90, 0);
Vector3's are just a data structure that contain 3 numbers, so you can use them to represent a lot of different things.
You can use them to represent an objects position: transform.position is a Vector3 that represents how far an object is from the world origin along each world axis.
You can use them to represent direction. Vector3.forward represents the World Z axis, which is the default forward direction. Vector3.forward is always the same, and it is (0, 0, 1). You can also have direction vectors that change based off of an objects orientation. For example, transform.forward represents the forward direction of the object, relative to world axis. This is not the same as the rotation, but it IS changed by rotation. For example the transform.forward of an object that has the default rotation is (0, 0, 1), or in other words the objects local forward direction matches up with the Global forward direction. If an object if facing backwards, its forward vector is (0, 0, -1). If an object is facing to the right it is (1, 0, 0) etc. Vector3.forward is a pure direction meaning that it has a length of 1.
If you want to move an object in a direction you can add a direction vector to its position vector, like in your code above. That's all that is happening, there are no Euler Angles in your code above. You are just seeing different applications of a collection of 3 numbers. You will get into trouble if you try to use Vectors that represent direction as a rotation Vector, or if you use a rotation vector as a position vector (unless you really really know what you are doing).
TLDR: There is no relation between the Vector3 in your code snippet and in the Vector3 used to set the Euler Angles in a rotation. They are separate use cases for the Vector3 data structure.
Thank you very much, you helped me a lot.
Answer by eliteforcevn · Apr 09, 2021 at 03:18 AM
to convert to true vector degree in the inspector use
if(transform.localEulerAngles.x != lastXDegree)
{
lastXDegree = transform.localEulerAngles.x;
Debug.Log("transform.localEulerAngles.x = " + transform.localEulerAngles.x);
float xDegree = ((lastXDegree + 540) % 360) - 180;
Debug.Log("Convert X Degree = " + xDegree);
Debug.Log("Sin X = " + Mathf.Sin(xDegree));
Debug.Log("Cos X = " + Mathf.Cos(xDegree));
}