public class rigidscript : MonoBehaviour { float dgr = 0; int power = 0; void Start () { } void Update () { // ボールの位置情報をposに入れる Vector3 pos = transform.position; // スペースバーを押した処理 if (Input.GetKeyDown (KeyCode.Space)) { power = 0; } // スペースバーを押している間の処理 if (Input.GetKey (KeyCode.Space)) { power++; } // スペースバーを離した処理。ボールを打つ。 if (Input.GetKeyUp (KeyCode.Space)) { // カメラ位置をvに入れる Vector3 v = Camera.main.transform.position; // カメラ位置からボール位置を引く v -= pos; // そのままだとボールからカメラに向かったベクトルになってしまうので、-1を掛けて力の向きを逆転させる v *= power * -1; // 加える力にy情報は不要なので0を代入 v.y = 0; transform.GetComponent<Rigidbody>().AddForce (v); } // 左矢印キーの処理。視点を回す if (Input.GetKey (KeyCode.LeftArrow)) { Camera.main.transform.RotateAround (pos, new Vector3 (0, 10, 0), -0.1f); dgr += 0.1f; } // 右矢印キーの処理。視点を回す if (Input.GetKey (KeyCode.RightArrow)) { Camera.main.transform.RotateAround (pos, new Vector3 (0, 10, 0), 0.1f); dgr -= 0.1f; } // カメラの新たな位置の計算と設定 float d = (2 * Mathf.PI) * (dgr / 360); float x = Mathf.Sin (d); float y = Mathf.Cos (d); x *= 10f; y *= 10f; pos.x += x; pos.y += 10f; pos.z -= y; Camera.main.transform.position = pos; } }
コメントをかく