前回まででゲームの根幹をなす部分はできました。
今回はプラスの要素としてコインがでて集めれるようにしましょう。
①コインの画像とコインを拾ったときの音を拾ってくる
http://taira-komori.jpn.org/game01.html
画像は一度シーンに配置しAssetsにドラック&ドロップしてPrehub化してください。
名前はcoinとします。
②CoinのPrehubにCircle Collider2Dをアタッチする。
CoinのPrehubにCircle Collider2Dをアタッチしてください。
そしてIs Triggerにチェックを入れてください。これで当たり判定だけを発生させ当たったときに力は発生しません。
②BlockCreater.csを改変する。
以下が改変したBlockCreater.csです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BlockCreater : MonoBehaviour { //BlockCreater.cs //改変後 public GameObject block; public GameObject Coin;//追加 public GameObject AllTrans; public float Allspeed; //ブロックが右に流れていくスピード public float timeOut; //何秒ごとにブロックがくるか private float timeElapsed; private int BlockValueOver; private int BlockValueUnder; private int CoinRundomValue;//追加 private int CoinRundomValueY;//追加 public float BlockBaseY;//画面端にくるブロックのY座標 4.5ぐらい public float BlockBaseX;//画面端にくるブロックのX座標 10ぐらい 大きければ遠くからくるから最初の波がくるのが遅くなる。 public float CoinBaseX;//追加 // Use this for initialization void Start () { } // Update is called once per frame void Update () { AllTrans.transform.position += new Vector3(-Allspeed, 0, 0); timeElapsed += Time.deltaTime; if (timeElapsed >= timeOut) { BlockValueOver = Random.Range(1, 5); for (int i = 0; i <= BlockValueOver; i++) { GameObject Blockclone = GameObject.Instantiate(block) as GameObject; //複製 Blockclone.transform.parent = AllTrans.transform; //BlockcloneをAllTransの子オブジェクトに Blockclone.transform.position = new Vector3(BlockBaseX, i - BlockBaseY, 0); //位置を調整 } BlockValueUnder = Random.Range(1, 2); for (int j = 0; j <= BlockValueUnder; j++) { GameObject Blockclone = GameObject.Instantiate(block) as GameObject; Blockclone.transform.parent = AllTrans.transform; Blockclone.transform.position = new Vector3(BlockBaseX, -j + BlockBaseY, 0); } CoinRundomValue = Random.Range(1, 4); for (int k = 0; k <= CoinRundomValue; k++) { CoinRundomValueY = Random.Range(1, 4); GameObject Coinclone = GameObject.Instantiate(Coin) as GameObject; Coinclone.tag = "coin"; Coinclone.transform.parent = AllTrans.transform; Coinclone.transform.position = new Vector3(CoinBaseX-k, CoinRundomValueY, 0); } timeElapsed = 0.0f; } } } |
スクリプトの値です。
CoinでcoinのPrehubを選択してください。
②コインに当たった時に音が鳴りコインを消すようにする
先ほどインポートした音源をシーンの鳥オブジェクトにアタッチしてください。
するとAudio Sourceというコンポーネントが追加されます。
Play On Awakeのチェックを外してください。
そしてシーンのオブジェクトにAudio Listenerを追加してください。追加されていればその一つで構いません。
コインに当たった時に音が鳴りコインを消すようにしましょう。
BirdController.csを改変します。
以下が改変したBirdController.csです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BirdController : MonoBehaviour { //BirdController.cs //改変後 public AudioClip CoinGetSound;//コイン音源 private AudioSource audioSource; public GameObject Bird; //とり。 public float JumpPower; //飛ぶ力。 // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) //マウスを右クリックしたら { Bird.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpPower)); //Birdに上向きに力が加わる。 } } public void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.tag == "coin") { audioSource = this.gameObject.GetComponent<AudioSource>(); audioSource.clip = CoinGetSound; audioSource.Play(); Destroy(other.gameObject); } } } |
以下が値です。
Coin Get Soundにコインの音源を選択してください。
いい感じですがコインの位置等はだいぶ適当なスクリプトです。
自分なりに変えてみて下さい。