Unity入門 - 超簡易版ビリヤード -
今回は、超簡易版のビリヤードを作ってみました。
Unityの物理エンジンを使えば、とても簡単に作ることができました。
(といってもゲーム性はほぼないですが。)
今回のビリヤートの概要は
・スクリプトはC#で作成
・カメラは、打つボールから一定距離を保ち、ボールを追跡する
・左右の矢印キーで打つ方向を操作
・スペースキーで打つ強さを操作(長押しで強くなる)
WebPlayer
Unity画面のスクリーンショット
手順
1. 盤面(Plane)を作成
2. 打つボール(Sphere)を作成
3. 周囲の枠(Cube)を作成
作成した枠にRigidbodyを追加し剛体特性を組み込む
4. ボール(Sphere)を10個作成する。
作成したボールにRigidbodyを追加し剛体特性を組み込む
5. 2,3,4で作成したHierarchyにPhysicsMaterialを適用し、物理特性を組み込む
6. 2で作成した打つボールに割り当てるスクリプトを作成
using UnityEngine; using System.Collections; public class rigidscript : MonoBehaviour { float dgr = 0; int power = 0; // Use this for initialization void Start () { } // Update is called once per frame void Update () { Vector3 pos = transform.position; // スペースキー操作 if (Input.GetKeyDown(KeyCode.Space)) { power = 0; } if (Input.GetKey(KeyCode.Space)) { power++; } if (Input.GetKeyUp(KeyCode.Space)) { Vector3 v = Camera.main.transform.position; v -= pos; v *= power * -1; v.y = 0; transform.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; } }
今回は、物理エンジンの使い方や、キーボード操作がメインで
得点、時間制限などゲーム性がほぼ実装されていないので、今後、機能追加していこうと思います!