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を取得し操作する、というやり方は、さまざまなオブジェクトを操作する際の基本となるやり方。
コメントをかく