Version: 2022.3
言語: 日本語
Scale Constraint
ライト

Unity の回転と向き

Unity uses a left-handed coordinate system. In Unity, you can use both Euler angles and quaternions to represent rotations and orientation. These representations are equivalent but have different uses and limitations.

通常、シーンのオブジェクトを回転させるには、オイラー角で方向を表示する Transform コンポーネントを使用します。ただし、Unity は回転と向きを内部的にクォータニオンとして保存します。これは、ジンバルロックにつながるかもしれないより複雑な動きに役立ちます。

Left-handed coordinate system

The axes of Unitys left-handed coordinate system, showing that the direction of rotation from the positive x-axis to the positive y-axis is counterclockwise when looking along the positive z-axis.
The axes of Unity’s left-handed coordinate system, showing that the direction of rotation from the positive x-axis to the positive y-axis is counterclockwise when looking along the positive z-axis.

A coordinate system describes the position of objects in a three-dimensional space. Unity uses a left-handed coordinate system: the positive x-axis points to the right, the positive y-axis points up, and the positive z-axis points forward. Unity’s left-handed coordinate system means that the direction of rotation from the positive x-axis to the positive y-axis is counterclockwise when looking along the positive z-axis.

オイラー角

Transform 座標では、Unity は回転をベクトルプロパティ Transform.eulerAngles X、Y、Z で表示します。通常のベクトルとは異なり、これらの値は実際に X、Y、Z 軸に関する回転角度 (度単位) を表します。

オイラー角の回転は、3 つの軸の周りに 3 つの別々の回転を実行します。Unity では、Z 軸、X 軸、Y 軸の順にオイラー角回転を行います。この回転方法は外的 (extrinsic) 回転で、回転が行われても元の座標系は変化しません。

ゲームオブジェクトを回転させるには、Transform コンポーネントに各軸をどれだけ回転させたいかという角度の値を入力します。スクリプトでゲームオブジェクトを回転させるには、Transform.eulerAngles を使います。オイラー角に変換して計算や回転を行うと、ジンバルロックの問題が発生する場合があります。

ジンバルロック

3D 空間にある物体が自由度を失い、2 次元内でしか回転できなくなることをジンバルロックといいます。ジンバルロックは、オイラー角で 2 つの軸が平行になると発生することがあります。 スクリプトで回転値をオイラー角に変換しない場合は、クォータニオンを使ってジンバルロックを防ぐことができます。

ジンバルロックの問題がある場合、回転に Transform.RotateAround を使用すれば、オイラー角を回避することができます。また、各軸に Quaternion.AngleAxis を使用し、それらを掛け合わせることもできます (クォータニオンの乗算は、各回転を順番に適用します)。

クォータ二オン

クォータニオンは、3D 空間における空間の向きや回転を表すための数学的表記法です。クォータニオンは 4 つの数値を使って、3D の単位軸の周りの回転方向と角度をエンコードします。これらの 4 つの値は方向や度数ではなく、複素数です。詳しくは、mathematics of quaternions を参照してください。

Unity converts rotational values to quaternions to store them because quaternion rotations are efficient and stable to compute. The Unity Editor doesn’t display rotations as quaternions because a single quaternion can’t represent a rotation greater than 360 degrees about any axis.

You can use quaternions directly if you use the Quaternion class. If you use script for your rotations, you can use the Quaternion class and functions to create and change rotational values. You can apply values to your rotation as Euler angles but you need to store them as quaternions to avoid problems.

オイラー角とクォータニオン間の変換

回転を好みの方法で表示編集するために、クォータニオンとオイラー角の間で変換するには、スクリプトを使用します。

  • オイラー角からクォータニオンへ変換するには、Quaternion.Euler 関数を使用します。
  • クォータニオンからオイラー角へ変換するには、Quaternion.eulerAngles 関数を使用します。

その他の参考資料

Scale Constraint
ライト