← 記事一覧に戻る

ブロック崩し2

ブロック崩し2

手順

1)ブロックを作る
2)プレハブにして全てブロックで配置
3)ボールを真上に飛ぶように作成
4)コインを作成
5)コインの当たり判定だけ実装

以降は課題にする

PhysicsMaterial2D

BounceMaterial:Bounciness=1, Friction=0
対象:上左右の壁、ボール

FloorMaterial:Bounciness=0, Friction=0
対象:床

スクリプト

StageGenerator.cs

csharp
1using UnityEngine; 2 3public class StageGenerator : MonoBehaviour 4{ 5 [SerializeField] private GameObject block; 6 [SerializeField] private GameObject coin; 7 [SerializeField] private int marginX; 8 [SerializeField] private int marginY; 9 [SerializeField] private int row; 10 [SerializeField] private int column; 11 12 void Awake() 13 { 14 var basePosition = transform.position; 15 for (int i = 0; i < row; i++) 16 { 17 int coinNumber = Random.Range(0, row); 18 for (int j = 0; j < column; j++) 19 { 20 Vector3 pos = basePosition + new Vector3(j * marginX, -i * marginY, 0); 21 if (j == coinNumber) 22 { 23 Instantiate(coin, pos, Quaternion.identity); 24 } 25 else 26 { 27 Instantiate(block, pos, Quaternion.identity); 28 } 29 } 30 } 31 } 32 33 // Update is called once per frame 34 void Update() 35 { 36 37 } 38}

BallState.cs
csharp
1public enum BallState 2{ 3 Idle, 4 Flying 5}

Ball.cs
csharp
1using TMPro; 2using UnityEngine; 3using UnityEngine.InputSystem; 4 5public class Ball : MonoBehaviour 6{ 7 [SerializeField] private float launchSpeed = 8f; 8 [SerializeField] private GameManager gameManager; 9 [SerializeField] private Rigidbody2D rigidbody2d; 10 [SerializeField] private TextMeshProUGUI scoreText; 11 12 private BallState state = BallState.Idle; 13 private int score; 14 15 void Start() 16 { 17 scoreText.text = "Score: " + score; 18 } 19 20 void Update() 21 { 22 if (state != BallState.Idle) return; 23 24 Pointer pointer = Pointer.current; 25 if (pointer == null || !pointer.press.wasPressedThisFrame) return; 26 27 Vector2 screenPos = pointer.position.ReadValue(); 28 Vector3 worldPos = Camera.main.ScreenToWorldPoint( 29 new Vector3(screenPos.x, screenPos.y, -Camera.main.transform.position.z)); 30 31 Vector2 dir = ((Vector2)worldPos - (Vector2)transform.position).normalized; 32 rigidbody2d.linearVelocity = dir * launchSpeed; 33 state = BallState.Flying; 34 } 35 36 void OnCollisionEnter2D(Collision2D collision) 37 { 38 if (collision.collider.CompareTag("Block")) 39 { 40 Destroy(collision.gameObject); 41 gameManager.NotifyBlockDestroyed(); 42 return; 43 } 44 45 if (state == BallState.Flying && collision.collider.CompareTag("Floor")) 46 { 47 rigidbody2d.linearVelocity = Vector2.zero; 48 state = BallState.Idle; 49 } 50 } 51 52 void OnTriggerEnter2D(Collider2D other) 53 { 54 if (other.gameObject.CompareTag("Coin")) 55 { 56 score++; 57 scoreText.text = "Score: " + score; 58 Destroy(other.gameObject); 59 } 60 } 61}

GameManager
csharp
1using UnityEngine; 2 3public class GameManager : MonoBehaviour 4{ 5 [SerializeField] private GameObject clearUIPanel; 6 7 private int remainingBlocks; 8 9 void Start() 10 { 11 clearUIPanel.SetActive(false); 12 remainingBlocks = GameObject.FindGameObjectsWithTag("Block").Length; 13 } 14 15 public void NotifyBlockDestroyed() 16 { 17 remainingBlocks--; 18 if (remainingBlocks <= 0) 19 { 20 clearUIPanel.SetActive(true); 21 } 22 } 23} 24