3Dオブジェクトを回転させたい。
回転を変換行列として扱いたい。
実は、回転行列を使うことで、3D回転を数学的に正確に表現できます。
回転行列は、3D空間での回転を、3×3または4×4行列で表現したもんです。
各軸での回転を、行列の積で組み合わせることができますね。
変換行列として扱えるため、座標変換などに応用できます。
この記事では、回転行列をゲームで使う理由について、Unity実装例とともに解説します。
- 回転行列とは何か、よく分からない…
- 3D回転を行列で表現したい。
- 回転を変換行列として扱いたい。
✨ この記事でわかること
- 回転行列の基本概念
- 各軸での回転行列
- 複数軸の回転の組み合わせ
- Unityでの実装方法
- 初心者でも理解できる回転行列の考え方
Unity入門の森をチェック Unity初心者でも安心。動画解説+完成サンプル付きで実装まで進められます
回転行列とは何か(ゲーム制作目線)

回転行列は、3D空間での回転を、行列で表現したものです。
3×3行列または4×4行列で表現します。
回転行列の特徴は、数学的に正確な回転表現と、変換の合成が容易であることです。
各軸(X軸、Y軸、Z軸)での回転を、個別の行列で表現できますね。
複数の回転を組み合わせる場合、行列の積で計算できますね。
Unityでは、Matrix4x4やQuaternionを使って、回転を扱えます。
ゲームでの具体的な使い道

回転行列が、ゲームでどう使われているか確認してみましょう。
オブジェクトの回転
3Dオブジェクトを、指定した角度で回転させます。
X軸、Y軸、Z軸の回転を組み合わせて、任意の方向に回転できます。
カメラの回転
カメラの向きを、回転行列で計算します。
マウス入力などに応じて、回転行列を更新します。
座標変換
ローカル座標とワールド座標の変換で、回転行列を使います。
親オブジェクトの回転を、子オブジェクトに適用します。
ベクトルの回転
方向ベクトルを、回転行列で回転させます。
速度ベクトルや法線ベクトルなど、様々なベクトルを回転できます。
- オブジェクトの回転(3Dモデルの向き)
- カメラの回転(視点の制御)
- 座標変換(ローカル・ワールド座標)
- ベクトルの回転(速度、法線など)
作り方は分かった。
でも完成まで行けない人へ
当たり判定・移動・カメラ・AIまで、
実装しながら学べる「永久会員チケット」です。
※ まずは内容を見るだけでOK
考え方・仕組みを図解イメージで説明

回転行列は、「各軸での回転行列 → 行列の積 → 合成された回転行列」という流れで実現できます。
X軸での回転
X軸周りの回転行列は、sinとcosを使って計算します。
Y軸とZ軸が回転し、X軸は変化しません。
Y軸での回転
Y軸周りの回転行列も、sinとcosで計算します。
X軸とZ軸が回転し、Y軸は変化しません。
Z軸での回転
Z軸周りの回転行列も、sinとcosで計算します。
X軸とY軸が回転し、Z軸は変化しません。
複数軸の組み合わせ
複数の回転を組み合わせる場合、行列の積で計算します。
順番が重要で、異なる順番で掛けると、結果が変わります。
- 回転行列は、3D空間での回転を行列で表現したもの
- 各軸での回転を、個別の行列で表現できる
- 複数の回転を組み合わせる場合、行列の積で計算する
- 回転の順番が重要で、結果が変わる
Unityで実装する際の注意点(代表例)

Unityで回転行列を扱う場合の注意点を見ていきましょう。
Matrix4x4を使った回転
UnityのMatrix4x4構造体で、回転行列を作成します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class RotationMatrixExample : MonoBehaviour { void Start() { // Y軸周りに45度回転 Quaternion rotation = Quaternion.Euler(0, 45, 0); Matrix4x4 rotationMatrix = Matrix4x4.Rotate(rotation); // ベクトルを回転 Vector3 originalVector = Vector3.forward; Vector3 rotatedVector = rotationMatrix.MultiplyVector(originalVector); Debug.Log($"元のベクトル: {originalVector}, 回転後: {rotatedVector}"); } } |
各軸での回転行列
各軸での回転行列を、手動で作成します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public static Matrix4x4 CreateRotationXMatrix(float angle) { float rad = angle * Mathf.Deg2Rad; float cos = Mathf.Cos(rad); float sin = Mathf.Sin(rad); Matrix4x4 matrix = Matrix4x4.identity; matrix.m11 = cos; matrix.m12 = -sin; matrix.m21 = sin; matrix.m22 = cos; return matrix; } public static Matrix4x4 CreateRotationYMatrix(float angle) { float rad = angle * Mathf.Deg2Rad; float cos = Mathf.Cos(rad); float sin = Mathf.Sin(rad); Matrix4x4 matrix = Matrix4x4.identity; matrix.m00 = cos; matrix.m02 = sin; matrix.m20 = -sin; matrix.m22 = cos; return matrix; } public static Matrix4x4 CreateRotationZMatrix(float angle) { float rad = angle * Mathf.Deg2Rad; float cos = Mathf.Cos(rad); float sin = Mathf.Sin(rad); Matrix4x4 matrix = Matrix4x4.identity; matrix.m00 = cos; matrix.m01 = -sin; matrix.m10 = sin; matrix.m11 = cos; return matrix; } |
複数軸の回転の組み合わせ
複数の回転を組み合わせて、1つの回転行列にします。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class CombinedRotation : MonoBehaviour { void Start() { // 各軸での回転角度 float rotationX = 30f; float rotationY = 45f; float rotationZ = 60f; // 各軸の回転行列を作成 Matrix4x4 rotX = CreateRotationXMatrix(rotationX); Matrix4x4 rotY = CreateRotationYMatrix(rotationY); Matrix4x4 rotZ = CreateRotationZMatrix(rotationZ); // 回転を組み合わせ(順番が重要:Z→Y→X) Matrix4x4 combinedRotation = rotX * rotY * rotZ; // ベクトルを回転 Vector3 originalVector = Vector3.forward; Vector3 rotatedVector = combinedRotation.MultiplyVector(originalVector); Debug.Log($"回転後: {rotatedVector}"); } // CreateRotationXMatrix, CreateRotationYMatrix, CreateRotationZMatrix // は前の例と同じ } |
QuaternionからMatrix4x4への変換
Quaternionから、回転行列に変換します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class QuaternionToMatrix : MonoBehaviour { void Start() { Quaternion quaternion = Quaternion.Euler(30, 45, 60); // QuaternionからMatrix4x4に変換 Matrix4x4 rotationMatrix = Matrix4x4.Rotate(quaternion); // ベクトルを回転 Vector3 vector = Vector3.forward; Vector3 rotated = rotationMatrix.MultiplyVector(vector); } } |
ベクトルの回転
回転行列を使って、ベクトルを回転させます。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class VectorRotation : MonoBehaviour { public Transform target; void Update() { // 回転行列を取得 Matrix4x4 rotationMatrix = Matrix4x4.Rotate(target.rotation); // 速度ベクトルを回転 Vector3 velocity = Vector3.forward * 5f; Vector3 rotatedVelocity = rotationMatrix.MultiplyVector(velocity); // オブジェクトを移動 transform.position += rotatedVelocity * Time.deltaTime; } } |
座標変換での使用
回転行列を使って、座標を変換します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class CoordinateRotation : MonoBehaviour { public Transform parent; public Vector3 localPosition; void Update() { // 親の回転行列を取得 Matrix4x4 rotationMatrix = Matrix4x4.Rotate(parent.rotation); // ローカル座標をワールド座標に変換 Vector3 worldPosition = parent.position + rotationMatrix.MultiplyVector(localPosition); transform.position = worldPosition; } } |
回転の順番の重要性
回転の順番によって、結果が変わることを示します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class RotationOrder : MonoBehaviour { void Start() { float rotX = 30f; float rotY = 45f; // X→Yの順 Matrix4x4 rotXY = CreateRotationYMatrix(rotY) * CreateRotationXMatrix(rotX); // Y→Xの順 Matrix4x4 rotYX = CreateRotationXMatrix(rotX) * CreateRotationYMatrix(rotY); Vector3 vector = Vector3.forward; Vector3 resultXY = rotXY.MultiplyVector(vector); Vector3 resultYX = rotYX.MultiplyVector(vector); Debug.Log($"X→Y: {resultXY}, Y→X: {resultYX}"); // 異なる結果 } // CreateRotationXMatrix, CreateRotationYMatrixは前の例と同じ } |

まとめ

この記事では、回転行列をゲームで使う理由について見てきました。
重要なポイントをおさらいします。
- 回転行列は、3D空間での回転を行列で表現したもの
- 各軸での回転を、個別の行列で表現できる
- 複数の回転を組み合わせる場合、行列の積で計算する
- UnityのMatrix4x4構造体で簡単に扱える
- 回転の順番が重要で、結果が変わる
回転行列は、3D回転を数学的に正確に表現するための重要な技術です。
各軸での回転を組み合わせることで、任意の方向に回転できます。
変換行列として扱えるため、座標変換などにも応用できます。
実際のゲーム実装とセットで学ぶことで、理解が深まるはずです。
Unity入門の森では、回転行列を含むベクトルと行列を、実際のゲーム実装とともに体系的に学べます。
ぜひチェックしてみてください。
Unity入門の森をチェック Unity初心者でも安心。動画解説+完成サンプル付きで実装まで進められます





コメント