Transformコンポーネントとは
![transform1](https://sunagitsune.com/wp-content/uploads/2021/01/transform1-1-650x297.jpg)
すべてのゲームオブジェクトはTransformコンポーネントを持ち、オブジェクトの位置、回転、拡大/縮小を数値で指定することができます。
![transform2](https://sunagitsune.com/wp-content/uploads/2021/01/transform2-1-650x319.jpg)
位置(Position)でオブジェクトの配置位置、
![transform3](https://sunagitsune.com/wp-content/uploads/2021/01/transform3-1-650x312.jpg)
回転(Rotation)でオブジェクトの回転具合(x軸を中心に横回転、360で一回転)、
![transform4](https://sunagitsune.com/wp-content/uploads/2021/01/transform4-1-650x312.jpg)
拡大/縮小(Scale)でオブジェクトのサイズを変更できます。
![transform5](https://sunagitsune.com/wp-content/uploads/2021/01/transform5-1-650x309.jpg)
x軸とy軸はそれぞれ横軸と縦軸、
![transform6](https://sunagitsune.com/wp-content/uploads/2021/01/transform6-1-650x298.jpg)
z軸は奥行きを意味します。
位置(Position)
![transform1](https://sunagitsune.com/wp-content/uploads/2021/07/transform1.jpg)
実際にオブジェクトの位置を変更してみましょう。まずは2Dの場合。
グリッドの上にオブジェクトがひとつだけ置かれている状態で、位置はxyzすべて0に設定してあります。
グリッドの上にオブジェクトがひとつだけ置かれている状態で、位置はxyzすべて0に設定してあります。
![transform2](https://sunagitsune.com/wp-content/uploads/2021/07/transform2.jpg)
オブジェクトを選択してインスペクターを表示し、位置のxを0から1に変更すると、
![transform3](https://sunagitsune.com/wp-content/uploads/2021/07/transform3.jpg)
背景の目盛り(グリッド)ひとつ分だけ、オブジェクトが右に動くことが確認できます。
![transform4](https://sunagitsune.com/wp-content/uploads/2021/07/transform4.jpg)
同様に位置のyを0から1に変更してみます。
![transform5](https://sunagitsune.com/wp-content/uploads/2021/07/transform5.jpg)
オブジェクトが目盛りひとつ分上に移動しました。
このように、2Dゲームではxが増えれば右、yが増えれば上にオブジェクトが移動します。
また、数字1につき目盛りひとつ分の距離を意味します。小数も指定可能です。
このように、2Dゲームではxが増えれば右、yが増えれば上にオブジェクトが移動します。
また、数字1につき目盛りひとつ分の距離を意味します。小数も指定可能です。
![3d unity transform position 1](https://sunagitsune.com/wp-content/uploads/2021/07/3d-unity-transform-position-1.png)
3Dの場合も同様ですが、xが右か左か奥行きかはカメラの位置によります。xを1増やせば、
![3d unity transform position 2](https://sunagitsune.com/wp-content/uploads/2021/07/3d-unity-transform-position-2.png)
x軸の方向に1目盛り分動きます。y軸は上下となります。
軸の方向は右上のギズモで確認することができます。
軸の方向は右上のギズモで確認することができます。
スクリプトで位置変更(transform.Translate)
オブジェクトを移動する
オブジェクト.transform.Translate(x, y, z);
オブジェクト.transform.Translate(x, y, z);
![](https://sunagitsune.com/wp-content/uploads/2021/06/cyclist-4857820_1280-160x90.jpg)
Unityでオブジェクトを移動する方法/transform.Translateの使い方
transformコンポーネントのTranslateを変更することで、オブジェクトの位置を変更することが可能です。
スクリプトでオブジェクトを「移動」させる場合、transform.Translateを使います。
//Unity C#
//マウスクリック時に実行
void OnMouseDown()
{
//x方向に1移動
//(数字はそれぞれx,y,z)
this.transform.Translate(1, 0, 0);
}
コードとしてはこのようになり、
![transform translate 1](https://sunagitsune.com/wp-content/uploads/2021/07/transform-translate-1.gif)
実行するとクリックしたオブジェクトがx方向に1動きます。
//Unity C#
this.transform.Translate(1.5, 0, 0);
ただし、transform.Translateの値に直接小数を入力はできません。整数の場合は省略できますが、transformの値はfloat値で指定する必要があり、float値であることを示すためには後ろにfを付ける必要があります(fを付けない小数はdoubleという型の扱いになる)。
![transform translate float](https://sunagitsune.com/wp-content/uploads/2021/07/transform-translate-float.png)
this.transform.Translate(1.5f, 0, 0);のように、数字の最後にfを付ければエラーが起きず、1.5目盛り動きます。
スクリプトでなく、インスペクターのTransformコンポーネントで直接小数を入力する時は、fを付ける必要はありません。
スクリプトでなく、インスペクターのTransformコンポーネントで直接小数を入力する時は、fを付ける必要はありません。
スクリプトで位置取得(transform.position)
//Unity C#
Debug.Log(this.transform.position);
transform.positionを使用すると、オブジェクトの現在位置が取得できます。
実際に実行すると、
実際に実行すると、
![transform position unity 1](https://sunagitsune.com/wp-content/uploads/2021/07/transform-position-unity-1.png)
こんな具合でx,y,zがそれぞれ出力されます。
transform.positionは3つの数字の集合体(構造体)であり、ひとつの数値では表せないため、縦横奥の値を組み合わせたVector3構造体という型で取得されます。
transform.positionは3つの数字の集合体(構造体)であり、ひとつの数値では表せないため、縦横奥の値を組み合わせたVector3構造体という型で取得されます。
![transform position unity 2](https://sunagitsune.com/wp-content/uploads/2021/07/transform-position-unity-2.png)
transform.x、transform.yのように指定すると、3つの値のひとつだけを取り出すことができます。
![transform position unity 3](https://sunagitsune.com/wp-content/uploads/2021/07/transform-position-unity-3.png)
全部0だと正しいかわからないので、少しオブジェクトの位置を変えて、
![transform position unity 4](https://sunagitsune.com/wp-content/uploads/2021/07/transform-position-unity-4.png)
実行すると、xyzがそれぞれ取得できていることが確認できます。
![](https://sunagitsune.com/wp-content/uploads/2021/07/computer-1343393_1280-160x90.jpg)
やりたいことから逆引きするUNITYの使い方まとめ
Unityをやりたいことから学習していけるよう、機能・用途別にまとめたページです。C#の命令別の逆引きは現時点で作っていません。2019の時期に書き始めているので、それより前のバージョンについては言及しません。
コメント