Nanashi-softプログラマ専用Unityでゲーム開発


◇Unityでゲーム開発 -JavaScriptでゲームオブジェクトを操る-

Unityではモデルデータからカメラやライトに至るまで『ゲームオブジェクト』と言う単位で一まとめになっています
つまり,このゲームオブジェクトを自在に操る事ができるようになることが,ツールをマスターする早道になります

●ゲームオブジェクトを検索する

スクリプトを Inspectorに設定した,自身は「this」です(記述を省略する場合がほとんどです)

他のゲームオブジェクトは,探す必要があります
・オブジェクト名で探す方法
var obj1 : GameObject = GameObject.Find("「Hierarchyに表示されている名称」");

・タグで探す方法
var obj1 : GameObject = GameObject.FindWithTag("「InspectorのTagに表示されている名称」");

thisの代わりに,この「obj1」変数を使います

●位置・サイズ・角度を取得・設定する

ゲームオブジェクトには,世界全体に対する自分の位置と,自分と親子関係にあるオブジェクトとの位置があります
これらは,現在の値を取得することも,値を変更することも可能です

・ワールド位置
this.transform.position.x
this.transform.position.y
this.transform.position.z

・ワールド角度
this.transform.eulerAngles.x
this.transform.eulerAngles.y
this.transform.eulerAngles.z

・ワールドサイズ
this.transform.lossyScale.x
this.transform.lossyScale.y
this.transform.lossyScale.z

・ローカル位置
this.transform.localPosition.x
this.transform.localPosition.y
this.transform.localPosition.z

・ローカル角度
this.transform.localEulerAngles.x
this.transform.localEulerAngles.y
this.transform.localEulerAngles.z

・ローカルサイズ
this.transform.localScale.x
this.transform.localScale.y
this.transform.localScale.z

●動かす

Inspectorの値を直接操作しても良いですが,もっと簡単にアニメーションさせる為の命令があります

・移動
this.transform.Translate(「X軸方向」,「Y軸方向」,「Z軸方向」);

・回転
this.transform.Rotate(「X軸方向」,「Y軸方向」,「Z軸方向」);

・サイズ
ない?

・目標のオブジェクトに向く
this.transform.LookAt(「オブジェクト」);

例)player1targetオブジェクトの方に向く
	var player1target : GameObject = GameObject.Find("player1target");
	this.transform.LookAt(player1target.transform.position);


TOPプログラマ専用Unityでゲーム開発