【学習】Transform クラス(位置・回転・拡大縮小)
Unity 入門シリーズでは、
- GameObject を配置した
- Transform で Position と Scale を設定した
という形でオブジェクトの配置を学びました。
しかし、Transform には他にも便利なプロパティやメソッドがあります。
Transform クラス
の主な API をまとめて押さえましょう。
今回は Transform のよく使うプロパティとメソッドを解説します。
今日学ぶこと
- position / localPosition … ワールド座標・ローカル座標
- rotation / localRotation … 回転
- scale / localScale … 拡大縮小
- Translate … 相対的に移動する
- 親子関係 … parent、SetParent、childCount
Transform とは
Transform は、すべての GameObject が持つコンポーネントです。位置・向き・大きさを制御します。MonoBehaviour を継承したスクリプトでは、transform プロパティでアクセスできます。
// 自分自身の Transform にアクセス
transform.position = new Vector3(5, 2, 0);
position(ワールド座標)
position は、シーン全体での絶対位置(ワールド座標)です。
// 位置を設定
transform.position = new Vector3(3, 2, 0);
// 現在の位置を取得
Vector3 pos = transform.position;
float x = transform.position.x;
| プロパティ | 型 | 説明 |
|---|---|---|
| position | Vector3 | ワールド座標での位置 |
| position.x | float | X 座標(右が正) |
| position.y | float | Y 座標(上が正) |
| position.z | float | Z 座標(奥が正) |
2D ゲームでは、Z は 0 のままにすることが多いです。
localPosition(ローカル座標)
localPosition は、親オブジェクトからの相対位置です。親子関係があるとき、親を基準にした位置を扱います。
// 親からの相対位置
transform.localPosition = new Vector3(1, 0, 0);
親がない場合は、position と localPosition は同じ値になります。
Translate(相対的に移動)
Translate は、現在の位置から指定した量だけ移動します。向き(rotation)を考慮するかどうかで挙動が変わります。
// 毎フレーム右に移動(ワールド座標で)
transform.Translate(1f * Time.deltaTime, 0, 0);
// 第2引数で Space を指定
transform.Translate(moveX, moveY, 0, Space.World); // ワールド座標
transform.Translate(moveX, moveY, 0, Space.Self); // 自分の向き基準(デフォルト)
| オーバーロード | 説明 |
|---|---|
| Translate(x, y, z) | 指定量だけ移動(Space.Self) |
| Translate(x, y, z, Space.World) | ワールド座標で移動 |
| Translate(x, y, z, Space.Self) | 自分の向きを基準に移動 |
rotation / localRotation(回転)
rotation はワールド座標での回転、localRotation は親からの相対回転です。型は Quaternion です。
// 回転なし
transform.rotation = Quaternion.identity;
// Z 軸で 90 度回転(2D)
transform.rotation = Quaternion.Euler(0, 0, 90);
// 2D で向きを変えるだけなら、localScale の X を -1 にする方法も
transform.localScale = new Vector3(-1, 1, 1); // 左右反転
scale / localScale(拡大縮小)
scale はワールド座標での大きさ、localScale は親からの相対的な大きさです。
// 2倍に拡大
transform.localScale = new Vector3(2, 2, 1);
// 左右反転(2D)
transform.localScale = new Vector3(-1, 1, 1);
2D では localScale の Z は 1 のままにすることが多いです。
親子関係
| プロパティ・メソッド | 説明 |
|---|---|
| parent | 親の Transform。なければ null |
| childCount | 子の数 |
| SetParent(Transform parent) | 親を設定する |
| GetChild(int index) | 指定インデックスの子を取得 |
// 親を設定
transform.SetParent(otherTransform);
// 親を解除(ワールドに出す)
transform.SetParent(null);
// 子の数を取得
int count = transform.childCount;
完成コード例(Translate で移動)
using UnityEngine;
public class MoveSample : MonoBehaviour
{
[SerializeField] private float speed = 5f;
void Update()
{
// 毎フレーム右に移動(入力は Input System などで取得した値を使う)
transform.Translate(speed * Time.deltaTime, 0, 0, Space.World);
}
}
重要ポイント
Transform は位置・回転・拡大縮小を制御するコンポーネントです。
- position … ワールド座標。直接設定して配置
- localPosition … 親からの相対位置
- Translate … 相対的に移動。Space.World / Space.Self で座標系を指定
- localScale … 大きさ。左右反転は X を -1 に
- SetParent … 親子関係を設定
発展アイデア
- Rigidbody2D … 物理演算で動かす場合は AddForce や velocity を使う
- 親子関係 … キャラの「体」と「腕」をまとめて動かす
- LookAt … 3D で特定の方向を向かせる(2D では使わないことが多い)
Transform は Unity の基本中の基本 です。位置や移動の操作はほぼすべて Transform が担います。


ディスカッション
コメント一覧
まだ、コメントがありません