Unity5学習の際の覚え書き。

×

名前を指定してゲームオブジェクトを取り出す「Find」メソッド

GameObject 変数 = GameObject.Find( 名前 );

ボールを追跡表示するカメラのスクリプト

public class rigidscript : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey (KeyCode.UpArrow)) {
			transform.GetComponent<Rigidbody>().AddForce (0, 0, 1);
		}
		if (Input.GetKey (KeyCode.DownArrow)) {
			transform.GetComponent<Rigidbody>().AddForce (0, 0, -1);
		}
		if (Input.GetKey (KeyCode.RightArrow)) {
			transform.GetComponent<Rigidbody>().AddForce (1, 0, 0);
		}
		if (Input.GetKey (KeyCode.LeftArrow)) {
			transform.GetComponent<Rigidbody>().AddForce (-1, 0, 0);
		}

		// ボールの位置情報をpositionで取得
		Vector3 pos = transform.position;
		// 取得したVector3の値を調整して、カメラ位置を設定
		// ここでは上(y軸)に2.5、手前(z軸)に3移動した位置にしている
		pos.y += 2.5f;
		pos.z -= 3f;
		GameObject camera = GameObject.Find ("Main Camera");
		camera.transform.position = pos;
	}
}

FindでGameObjectを取得し操作する、というやり方は、さまざまなオブジェクトを操作する際の基本となるやり方。


タグを指定してゲームオブジェクトを取り出す「FindGameObjectWithTag」メソッド

GameObject 変数 = GameObject.FindGameObjectWithTag( タグ名 );

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu

【メニュー編集】
Wiki記法ガイド

リンク

ゲームバー大阪心斎橋

ゲームバー大阪梅田

ゲームバー大阪心斎橋

ダーツ&ダイニングバー大阪梅田

メニュー

Unity


スマホ操作

Unity - GameObject

Unity - 3DCG

メンバーのみ編集できます