Prefab Power!
Unity Breakout
Breakout is Unity’s perfect prefab lesson: build one brick, stamp out fifty from code, wire up physics bounces, particle bursts and a proper game-state manager with restart.
Breakout is Unity’s perfect prefab lesson: build one brick, stamp out fifty from code, wire up physics bounces, particle bursts and a proper game-state manager with restart.
Set up the arena and a paddle that tracks the mouse.
using UnityEngine;
public class PaddleMouse : MonoBehaviour
{
void Update()
{
Vector3 world = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(
Mathf.Clamp(world.x, -7f, 7f),
-4.2f,
0f);
}
}
Create a brick GameObject and turn it into a prefab.
using UnityEngine;
public class Brick : MonoBehaviour
{
public int points = 10;
public void SetRow(int row, Color color)
{
points = (5 - row) * 10;
GetComponent<SpriteRenderer>().color = color;
}
}
Generate the whole wall from code using the prefab.
using UnityEngine;
public class WallBuilder : MonoBehaviour
{
public Brick brickPrefab;
public Color[] rowColors = new Color[5];
void Start()
{
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 10; col++)
{
Vector3 pos = new Vector3(-6.75f + col * 1.5f,
4f - row * 0.6f, 0f);
Brick b = Instantiate(brickPrefab, pos, Quaternion.identity);
b.SetRow(row, rowColors[row]);
b.transform.parent = transform; // tidy hierarchy
}
}
}
}
Brick b = (brickPrefab, pos, Quaternion.identity); b.(row, rowColors[row]);
Wire the ball’s physics and destroy bricks on contact.
using UnityEngine;
public class Ball : MonoBehaviour
{
public float speed = 6f;
Rigidbody2D rb;
void OnCollisionEnter2D(Collision2D hit)
{
Brick brick = hit.gameObject.GetComponent<Brick>();
if (brick != null)
{
GameManager.Instance.AddScore(brick.points);
Destroy(brick.gameObject);
}
if (hit.gameObject.CompareTag("Paddle"))
{
float offset = transform.position.x - hit.transform.position.x;
Vector2 dir = new Vector2(offset * 1.2f, 1f).normalized;
rb.linearVelocity = dir * speed;
}
}
}
Add Unity’s particle system for brick-break explosions.
public ParticleSystem burstPrefab;
// inside the brick-hit branch:
ParticleSystem burst = Instantiate(
burstPrefab,
brick.transform.position,
Quaternion.identity);
var main = burst.main;
main.startColor = brick.GetComponent<SpriteRenderer>().color;
Finish with lives, win/lose states and a restart that just works.
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
void Awake() { Instance = this; }
public void Restart()
{
Time.timeScale = 1f;
SceneManager.LoadScene(
SceneManager.GetActiveScene().name);
}
}
SceneManager.(
SceneManager.().name);
Keep the Unity cheatsheet handy
MonoBehaviour, physics and UI in one place - use it beside this workshop.
Now skin your Unity Breakout!
Design your paddle, ball, and bricks in Pixel Studio - then import them into your Unity project.
Vector2 movement = new Vector2(moveX, 0);void Update() {}float moveX = Input.GetAxis("Horizontal");rb.velocity = movement * speed;