Menu
CoddyTech

Three Balls

Lesson 17 of 19 in Coddy's Unity Game: Brick Breaker course.

A Multiball power-up splits the ball: two copies fly off from where it is. Instantiate copies a scene object the way it copies a prefab, but only what the Inspector would save comes across: public and [SerializeField] fields such as speed and paddle. A private field like launched starts at false, and the velocity at zero, so a copy must be launched or it rides the paddle.

Ball copy = Instantiate(source, source.transform.position, Quaternion.identity);
copy.Launch(new Vector3(0.2f, 1f, 0f));

The GameManager keeps every ball in play in a List<Ball>, so it can find them later.

challenge icon

Challenge

Hard

Catch a Multiball power-up and the ball splits in three.

  1. In GameManager, add List<Ball> balls = new List<Ball>(); (with using System.Collections.Generic;); Start() adds ball to it first.
  2. Write public void SplitBall(): two copies of balls[0] at its position, launched toward (-0.6f, 1f, 0f) and (0.6f, 1f, 0f), both added to balls.
  3. In PowerUp, a Multiball calls GameManager.Instance.SplitBall().

Press Play and catch a pink one: three balls fly. Then Check.

Try it yourself

using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public Brick brickPrefab;
    public Transform wall;
    public string[] levels;
    public float spacingX = 1.75f;
    public float spacingY = 0.75f;
    public float topY = 6f;

    public Ball ball;
    public int lives = 3;
    public TMP_Text scoreText;
    public TMP_Text livesText;
    public GameObject gameOverPanel;
    public GameObject[] powerUps;
    public float dropChance = 0.15f;

    int score;
    int level;
    int bricksLeft;

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        BuildLevel(0);
        ShowHud();
    }

    void BuildLevel(int index)
    {
        level = index;
        string[] rows = levels[index].Split('/');
        for (int row = 0; row < rows.Length; row++)
        {
            string line = rows[row];
            float left = -(line.Length - 1) * spacingX / 2f;
            for (int col = 0; col < line.Length; col++)
            {
                char c = line[col];
                if (c == '.') continue;
                Vector3 pos = new Vector3(left + col * spacingX, topY - row * spacingY, 0f);
                Brick brick = Instantiate(brickPrefab, pos, Quaternion.identity, wall);
                brick.SetHits(c - '0');
                bricksLeft++;
            }
        }
    }

    public void BrickBroken(Brick brick)
    {
        score += brick.points;
        bricksLeft--;
        ShowHud();
        if (Random.value < dropChance)
        {
            GameObject prefab = powerUps[Random.Range(0, powerUps.Length)];
            Instantiate(prefab, brick.transform.position, Quaternion.identity);
        }
        if (bricksLeft == 0)
        {
            ball.ResetToPaddle();
            BuildLevel((level + 1) % levels.Length);
        }
    }

    public void BallLost(Ball lost)
    {
        lives--;
        ShowHud();
        if (lives == 0)
        {
            gameOverPanel.SetActive(true);
            lost.gameObject.SetActive(false);
        }
        else lost.ResetToPaddle();
    }

    public void PlayAgain()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    void ShowHud()
    {
        scoreText.text = "Score: " + score;
        livesText.text = "Lives: " + lives;
    }
}

All lessons in Unity Game: Brick Breaker

Practice on your own: Unity playground