今日はクソゲーを作ってみた。
上から降ってくる障害物を押しのけるゲーム。
ちょっと改良すれば、ゲーム&ウオッチのヘルメットができるかも。
ゲームはあれだが、覚えたこともあるので良かった。
下が完成したゲーム。
自分のために使ったスクリプトを貼り付けていく。
1.wallGenerator
一応要のスクリプト。
多くは過去の流用で下の記事の大砲の発射のスクリプトがほとんど。
sashimimayonezu.hatenablog.com
それに加えて降ってきた障害物をカウントして、
それによって落下速度、インターバルの時間を調整するようにした。
これで時間がたつにつれて難しくなるようになった。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class WallGenerator : MonoBehaviour { //Wallプレハブ public GameObject Wall; //弾が生成されるポジションを保有するゲームオブジェクト public GameObject WallPos; private int count=0; public Text scoretext; //弾丸のスピード public float speed = 1500f; //敵生成時間間隔 private float interval; //時間間隔の最小値 public float minTime = 0.5f; //時間間隔の最大値 public float maxTime = 0.9f; //経過時間 private float time = 0f; //X座標の最小値 public float xMinPosition = -15f; //X座標の最大値 public float xMaxPosition = 15f; // Start is called before the first frame update void Start() { //時間間隔を決定する interval = GetRandomTime(); scoretext.text = "Point:0"; } // Update is called once per frame void Update() { time += Time.deltaTime; //スペースが押されたとき if (time > interval) { //ballをインスタンス化して発射 GameObject createdWall = Instantiate(Wall) as GameObject; //生成した弾の位置をランダムに設定する createdWall.transform.position = GetRandomPosition(); //発射ベクトル Vector3 force; //発射の向きと速度を決定 force = WallPos.transform.forward * speed *(count+100)/100; // Rigidbodyに力を加えて発射 createdWall.GetComponent<Rigidbody>().AddForce(force); //時間リセット time = 0f; count += 1; SetCountText(); Debug.Log(count); //時間間隔を決定する interval = GetRandomTime(); } } //ランダムな時間を生成する関数 private float GetRandomTime() { minTime = 0.5f * 1000 / (1000 + count); maxTime = 0.9f * 1000 / (1000 + count); return Random.Range(minTime, maxTime); } //ランダムな位置を生成する関数 private Vector3 GetRandomPosition() { //それぞれの座標をランダムに生成する float x = Random.Range(xMinPosition, xMaxPosition); //Vector3型のPositionを返す return new Vector3(x, 10f, 1.3f); } // UI の表示を更新する void SetCountText() { // スコアの表示を更新 scoretext.text = "Point: " + count.ToString(); } }
2.Player
ただのプレイヤーの移動。
キー入力で移動する。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player: MonoBehaviour { // Rigidbodyコンポーネントを入れる変数"rb"を宣言する。 public Rigidbody rb; void Start() { // Rigidbodyコンポーネントを取得する rb = GetComponent<Rigidbody>(); } void FixedUpdate() { // Upキーで前に進む if (Input.GetKey(KeyCode.LeftArrow)) { rb.AddForce(-3f, 2f, 0, ForceMode.Impulse); } // Upキーで前に進む if (Input.GetKey(KeyCode.RightArrow)) { rb.AddForce(3f,2f, 0, ForceMode.Impulse); } // Upキーで前に進む if (Input.GetKey(KeyCode.DownArrow)) { rb.AddForce(0, -1f, 0, ForceMode.Impulse); } } }
3.GameOver
実は今回一番時間がかかった且つ為になった部分。
というのも他のオブジェクトにアタッチされたスクリプトを
OFFにする方法を探すのに時間がかかった。
もっといい方法があるのかもしれないがとりあえずできたので
これから使っていきたい。この一文を覚えておこう。
GameObject.Find("GameObject名").GetComponent<スクリプト名>().enabled = false;
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameOver : MonoBehaviour { public Text over; public void Start() { over.text = ""; } private void OnCollisionEnter(Collision collision) { Destroy(collision.gameObject); over.text="GameOver"; //スクリプトWallGeneratorがアタッチされているオブジェクトWallGenを見つけて //そのスクリプトをOFFにしている GameObject.Find("WallGen").GetComponent<WallGenerator>().enabled = false; } }
4.Delete
カメラから消えた物体を消すことができる。便利。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Delete : MonoBehaviour { // カメラに写っていないときに呼ばれる関数 void OnBecameInvisible() { GameObject.Destroy(this.gameObject); } }
5.Start
ゲームメインのシーンを呼び出すスクリプト。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Start : MonoBehaviour { /// ボタンをクリックした時の処理 public void OnClick() { Application.LoadLevel("Main"); // シーンの名前かインデックスを指定 } }
これでとりあえずゲーム完成。
Unityroomに上げます。
https://unityroom.com/games/barrier222
Unityroomにせっかく挙げるなら、
スコア機能やツイッター機能も付けてみたいが
面倒なのでまた今度。