简体   繁体   中英

Is there a method that returns the y-component of a (rotation) quaternion in C# Unity?

So, in the Unity editor, the y-component of the rotation is shown in the transform tab.

It looks something like this in the editor: Rotation X[some number] Y[some number] Z[some number].

I wish to obtain the "some number" next to the Y, however, I seem to be unable to directly assign a quaternion to a double. It seems like there should be a method to quickly find this value, however, I was not able to find anything in the documentation. I need this value in order to perform simple sine and cosine on it.

Is there a simple method which returns one of these values as a double?

Quaternions consist of three imaginary components and one real component and are not meant to be directly edited. However, Unity's Quaternion class provides useful shortcuts that enable you to do so, take a look at the following example:

Vector3 asEuler = myQuaternion.eulerAngles;
asEuler.y = 123.5f;
myQuaternion = Quaternion.Euler(asEuler);

The firsts lines perform a convertion to Euler representation, thats the one you see in the inspector, it has x, y, and z which are all real (single) values. In the second line, there is an obvious assignment of values. Finally, in line three there is conversion back to quaternion from Euler representation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM