前提
5枚のブロック画像がランダムに(-7~8*50)配置されている
ブロックが上からスクロールしてくる
プレイヤーが下に表示されていて左右移動できる
実現したいこと
ブロックと当たり判定をとった時特定の条件が満たされたら範囲内のオブジェクトを消す処理が知りたい
もし配列の同じ画像が2枚以上揃っていたらプレイヤーに当たった時揃ったものをすべて消す
上の条件をループさせて連鎖消えを実現させる
発生している問題・エラーメッセージ
当たったら連結しているすべてのブロックが削除されてしまう。
該当のソースコード
追記
C#
//Playerにアタッチ public float playerSpeed; private Vector2 player_pos; public int width; public int height; public GameObject[] blocks; public GameObject[,] fieldBlocks; [SerializeField] List<GameObject> checkZumiFieldBlocks = new List<GameObject>(); GameObject piece; private int randomNum; void Start() { fieldBlocks = new GameObject[width, height]; } void Hairetu()// ブロックを作っているところ { for (int x = 0; x < width; x += 2) { for (int y = 0; y < height; y += 2) { randomNum = Random.Range(0, 5);// 0,1,2,3,4 piece = Instantiate(blocks[randomNum]) as GameObject; piece.transform.position = new Vector3(x, y, 0); fieldBlocks[x, y] = piece; } } } void Begin() { float moveX = Input.GetAxis("Horizontal") * Time.deltaTime * playerSpeed; float moveY = Input.GetAxis("Vertical") * Time.deltaTime * playerSpeed; transform.position = new Vector2( //エリア指定して移動する Mathf.Clamp(transform.position.x + moveX, 0.0f, 20.0f), Mathf.Clamp(transform.position.y + moveY, -4.0f, 18.0f) ); player_pos = transform.position; //プレイヤーの位置を取得 player_pos.x = Mathf.Clamp(player_pos.x, 0.0f, 20.0f); //x位置が常に範囲内か監視 player_pos.y = Mathf.Clamp(player_pos.y, -4.0f, 18.0f); //y位置が常に範囲内か監視 transform.position = new Vector2(player_pos.x, player_pos.y); public void Drop()// ブロックの落下処理 { int nullCount = 0; for (int x = 0; x < width; x += 2) { for (int y = 0; y < height; y += 2) { if (fieldBlocks[x, y] == null) { nullCount += 2; } else if (nullCount > 0) { fieldBlocks[x, y].transform.position += new Vector3(0, -nullCount, 0); fieldBlocks[x, y - nullCount] = fieldBlocks[x, y]; fieldBlocks[x, y] = null; } } nullCount = 0; } if (RenketuAri()) { StartCoroutine(Erase()); } } public bool RenketuAri()// 連結を確認 { for (int x = 0; x < width; x += 2) { for (int y = 0; y < height; y += 2) { checkZumiFieldBlocks.Clear(); if (Renketusuu(x, y, 0) > 1 && fieldBlocks[x, y] != null) { return true; } } } return false; } public IEnumerator Erase()// 1つ以上そろったら削除 { yield return new WaitForSeconds(0.5f); for (int x = 0; x < width; x += 2) { for (int y = 0; y < height; y += 2) { checkZumiFieldBlocks.Clear();// Listをクリア // 同じ種類が1以上だったら配列を削除 if (Renketusuu(x, y, 0) > 1 && fieldBlocks[x, y] != null) { Destroy(fieldBlocks[x, y]); } } } yield return new WaitForSeconds(0.5f); Drop(); } int Renketusuu(int x, int y, int rennketusuu)// 連結しているか調べる { if (fieldBlocks[x, y] == null || checkZumiFieldBlocks.Contains(fieldBlocks[x, y])) { return rennketusuu; } checkZumiFieldBlocks.Add(fieldBlocks[x, y]); rennketusuu++; if (x != width - 2 && fieldBlocks[x + 2, y] != null && fieldBlocks[x, y].name == fieldBlocks[x + 2, y].name)// 右 { Debug.Log(fieldBlocks[x + 2, y].name); rennketusuu = Renketusuu(x + 2, y, rennketusuu); } if (x != 0 && fieldBlocks[x - 2, y] != null && fieldBlocks[x, y].name == fieldBlocks[x - 2, y].name)// 左 { Debug.Log(fieldBlocks[x - 2, y].name); rennketusuu = Renketusuu(x - 2, y, rennketusuu); } if (y != 0 && fieldBlocks[x, y - 2] != null && fieldBlocks[x, y].name == fieldBlocks[x, y - 2].name)// 下 { Debug.Log(fieldBlocks[x, y - 2].name); rennketusuu = Renketusuu(x, y - 2, rennketusuu); } if (y != height - 2 && fieldBlocks[x, y + 2] != null && fieldBlocks[x, y].name == fieldBlocks[x, y + 2].name)// 上 { Debug.Log(fieldBlocks[x, y + 2].name); rennketusuu = Renketusuu(x, y + 2, rennketusuu); } return rennketusuu; } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "Block_B" || collision.gameObject.tag == "Block_G" || collision.gameObject.tag == "Block_Y" || collision.gameObject.tag == "Block_R") { for (int x = 0; x < width; x += 2) { for (int y = 0; y < height; y += 2) { // Listをクリア checkZumiFieldBlocks.Clear(); // 同じ種類が1以上だったら配列を削除 if (Renketusuu(x, y, 0) > 1 && fieldBlocks[x, y] != null) { Debug.Log(fieldBlocks[x, y] + "_" + "連結数_①"); Destroy(fieldBlocks[x, y]); } } } } }
試したこと
現在の状況
↑一つのコードでブロックの生成から削除までをやっています。プレイヤーが当たったら連結しているブロックをすべて消すことができました。ただ、これにプレイヤーが当たったブロックの連結数を調べ削除する条件を追加したいです。
補足情報(FW/ツールのバージョンなど)
2021.2.13f1
Unity2D(コア)
まだ回答がついていません
会員登録して回答してみよう