Quaternion and rotation angles

Good afternoon. I have the following expression:

 Quaternion myq = Quaternion.Euler(0, 0, zAn) * Quaternion.Euler(0, yAn, 0) * Quaternion(xAn, 0, 0);

Prompt how to solve the reverse problem. I have a value of a quaternion of myq, and it is necessary to find angles xAn, yAn, zAn(xAn=?, yAn=?, zAn=?). Maybe formula or function of Unity. Thanks in advance for the help.

Hello,

When using an API you are not used to, it’s a good idea to read through the documentation of something before you use it. Then you wouldn’t have to ask questions and waste your own time :slight_smile:

If you take a look at the documentation for Quaternion here, you can see that .Euler makes a new quaternion rotation with euler-angles, and the field eulerAngles gets the euler-angles from a quaternion.

Note that there are multiple euler-angles for every Quaternion so it might not be what you expect :slight_smile:

Hope this helps,
Benproductions1

As @Benproductions1 says, there are multiple euler angle representations for any given “physical” rotation. An eularAngles are derived from the Quaternion, so euler values are not saved. For example, run this code:

transform.eulerAngles = Vector3(180,0,0);
Debug.Log(transform.eulerAngles);

The value you will get back is (0,180,180)…the same physical rotation, but in a representation different then the one you set.

One solution is to preserve your own Vector3 and treat eulerAngles as write-only. For example:

public var speed = 20.0;
private var angles = Vector3.zero;

function Update() {
	angles.z += Input.GetAxis("Horizontal") * speed * Time.deltaTime;
	angles.x += Input.GetAxis("Vertical") * speed * Time.deltaTime;
	transform.eulerAngles = angles;
}

Note that changes are always made in ‘angles’ and ‘eulerAngles’ is then set (but never read). If I want the euler values, I can read them from ‘angles’. I can always manipulate or normalize ‘angles’ for whatever my task. This technique does not work for every situation.