前提
5枚のブロック画像がランダムに(-7~8*50)配置されている
ブロックが上からスクロールしてくる
プレイヤーが下に表示されていて左右移動できる
実現したいこと
ブロックと当たり判定をとった時特定の条件が満たされたら範囲内のオブジェクトを消す処理が知りたい
もし配列の同じ画像が2枚以上揃っていたらプレイヤーに当たった時揃ったものをすべて消す
上の条件をループさせて連鎖消えを実現させる
発生している問題・エラーメッセージ
当たったら連結しているすべてのブロックが削除されてしまう。
該当のソースコード
追記
C#
1//Playerにアタッチ 2public float playerSpeed; 3private Vector2 player_pos; 4public int width; 5public int height; 6public GameObject[] blocks; 7public GameObject[,] fieldBlocks; 8[SerializeField] List<GameObject> checkZumiFieldBlocks = new List<GameObject>(); 9GameObject piece; 10private int randomNum; 11void Start() 12{ 13 fieldBlocks = new GameObject[width, height]; 14} 15 16void Hairetu()// ブロックを作っているところ 17{ 18 for (int x = 0; x < width; x += 2) 19 { 20 for (int y = 0; y < height; y += 2) 21 { 22 randomNum = Random.Range(0, 5);// 0,1,2,3,4 23 piece = Instantiate(blocks[randomNum]) as GameObject; 24 piece.transform.position = new Vector3(x, y, 0); 25 fieldBlocks[x, y] = piece; 26 } 27 } 28} 29 30void Begin() 31 { 32 float moveX = Input.GetAxis("Horizontal") * Time.deltaTime * playerSpeed; 33 float moveY = Input.GetAxis("Vertical") * Time.deltaTime * playerSpeed; 34 35 transform.position = new Vector2( 36 //エリア指定して移動する 37 Mathf.Clamp(transform.position.x + moveX, 0.0f, 20.0f), 38 Mathf.Clamp(transform.position.y + moveY, -4.0f, 18.0f) 39 ); 40 41 player_pos = transform.position; //プレイヤーの位置を取得 42 43 player_pos.x = Mathf.Clamp(player_pos.x, 0.0f, 20.0f); //x位置が常に範囲内か監視 44 player_pos.y = Mathf.Clamp(player_pos.y, -4.0f, 18.0f); //y位置が常に範囲内か監視 45 transform.position = new Vector2(player_pos.x, player_pos.y); 46 47public void Drop()// ブロックの落下処理 48{ 49 int nullCount = 0; 50 for (int x = 0; x < width; x += 2) 51 { 52 for (int y = 0; y < height; y += 2) 53 { 54 if (fieldBlocks[x, y] == null) 55 { 56 nullCount += 2; 57 } 58 else if (nullCount > 0) 59 { 60 fieldBlocks[x, y].transform.position += new Vector3(0, -nullCount, 0); 61 fieldBlocks[x, y - nullCount] = fieldBlocks[x, y]; 62 fieldBlocks[x, y] = null; 63 } 64 } 65 nullCount = 0; 66 } 67 if (RenketuAri()) 68 { 69 StartCoroutine(Erase()); 70 } 71} 72 73public bool RenketuAri()// 連結を確認 74{ 75 for (int x = 0; x < width; x += 2) 76 { 77 for (int y = 0; y < height; y += 2) 78 { 79 checkZumiFieldBlocks.Clear(); 80 if (Renketusuu(x, y, 0) > 1 && fieldBlocks[x, y] != null) 81 { 82 return true; 83 } 84 } 85 } 86 return false; 87} 88 89public IEnumerator Erase()// 1つ以上そろったら削除 90{ 91 yield return new WaitForSeconds(0.5f); 92 for (int x = 0; x < width; x += 2) 93 { 94 for (int y = 0; y < height; y += 2) 95 { 96 checkZumiFieldBlocks.Clear();// Listをクリア 97 // 同じ種類が1以上だったら配列を削除 98 if (Renketusuu(x, y, 0) > 1 && fieldBlocks[x, y] != null) 99 { 100 Destroy(fieldBlocks[x, y]); 101 } 102 } 103 } 104 yield return new WaitForSeconds(0.5f); 105 Drop(); 106} 107 108int Renketusuu(int x, int y, int rennketusuu)// 連結しているか調べる 109{ 110 if (fieldBlocks[x, y] == null || checkZumiFieldBlocks.Contains(fieldBlocks[x, y])) 111 { 112 return rennketusuu; 113 } 114 checkZumiFieldBlocks.Add(fieldBlocks[x, y]); 115 116 rennketusuu++; 117 118 if (x != width - 2 && fieldBlocks[x + 2, y] != null && fieldBlocks[x, y].name == fieldBlocks[x + 2, y].name)// 右 119 { 120 Debug.Log(fieldBlocks[x + 2, y].name); 121 rennketusuu = Renketusuu(x + 2, y, rennketusuu); 122 } 123 if (x != 0 && fieldBlocks[x - 2, y] != null && fieldBlocks[x, y].name == fieldBlocks[x - 2, y].name)// 左 124 { 125 Debug.Log(fieldBlocks[x - 2, y].name); 126 rennketusuu = Renketusuu(x - 2, y, rennketusuu); 127 } 128 if (y != 0 && fieldBlocks[x, y - 2] != null && fieldBlocks[x, y].name == fieldBlocks[x, y - 2].name)// 下 129 { 130 Debug.Log(fieldBlocks[x, y - 2].name); 131 rennketusuu = Renketusuu(x, y - 2, rennketusuu); 132 } 133 if (y != height - 2 && fieldBlocks[x, y + 2] != null && fieldBlocks[x, y].name == fieldBlocks[x, y + 2].name)// 上 134 { 135 Debug.Log(fieldBlocks[x, y + 2].name); 136 rennketusuu = Renketusuu(x, y + 2, rennketusuu); 137 } 138 139 return rennketusuu; 140} 141 142private void OnCollisionEnter2D(Collision2D collision) 143{ 144 if (collision.gameObject.tag == "Block_B" || 145 collision.gameObject.tag == "Block_G" || 146 collision.gameObject.tag == "Block_Y" || 147 collision.gameObject.tag == "Block_R") 148 { 149 for (int x = 0; x < width; x += 2) 150 { 151 for (int y = 0; y < height; y += 2) 152 { 153 // Listをクリア 154 checkZumiFieldBlocks.Clear(); 155 // 同じ種類が1以上だったら配列を削除 156 if (Renketusuu(x, y, 0) > 1 && fieldBlocks[x, y] != null) 157 { 158 Debug.Log(fieldBlocks[x, y] + "_" + "連結数_①"); 159 Destroy(fieldBlocks[x, y]); 160 } 161 } 162 } 163 } 164}
試したこと
現在の状況
↑一つのコードでブロックの生成から削除までをやっています。プレイヤーが当たったら連結しているブロックをすべて消すことができました。ただ、これにプレイヤーが当たったブロックの連結数を調べ削除する条件を追加したいです。
補足情報(FW/ツールのバージョンなど)
2021.2.13f1
Unity2D(コア)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。