Arkandroid: Let’s make a Breakout Game with Unity3D! – Part 2

 


Welcome back!

In Part 1 of this series, I bet you noticed that something’s not quite right with the paddle.

You guessed it! The paddle shouldn’t really leave the screen, right? We gotta do something about it!

Or 4! Right now we want to keep the paddle from leaving the scene but once we add the ball we will have the same issue again. So why not solve it before it even appears?

First, we’re going to need a couple of bricks. Wait a second… 🤔

Oops, wrong instructions 😶

First, we’re going to create an empty game object.

There we go!

To do that, right-click on the Hierarchy window (the one to the left) and click on Create Empty. Press F2 on the keyboard to rename it and type WallLeft.

Great! Now we want to make it collide with the paddle so it won’t leave the scene. To do that we have to add a rigidbody and a collider. So let’s click our beloved Add Component button once more and search for Rigidbody 2D, add it and then repeat the same thing for Box Collider 2D. Then, on the Rigidbody we have to change the Body Type property from Dynamic to Static in order to keep it, well, static inside the game world. We don’t want any forces to be applied to it, the only reason we add it is to restrict other objects from leaving the scene! (This also means that the object won’t get any effects from gravity. Actually, once we set the Body Type to Static, the properties for gravity are gone.)

Alright, so we added our wall but right now it’s useles because we have to move it and change its size in order to enclose the viewport. We can do that using the Move and Scale tools from the toolbox on the top left part of the screen:

(Note: Whenever you start learning how to use a program, it’s really useful to also learn some shortcuts. This will really speed up your work and save you a ton of time! In this case, you can switch between these tools using the QWERTY keys, Q for the Hand Tool, W for the Move Tool, E for the Rotate Tool. Easy, right?)

First, use the Move Tool to move the wall to the left edge of the viewport:

Then, switch to the Scale Tool to change the wall’s height and cover the whole left side:

Okay, that’s one side covered. But we want to cover all for sides! So let’s copy the WallLeft object, rename it to WallRight and move it to the right side using the Move Tool.

Now if we hit play and try to move the paddle to the edges…

Success! But I still kinda feel bad for that paddle… Anyhoo…

We still need to close the top and bottom. You know the drill, copy, paste, rename, resize:

Great, now you destroyed all of paddle’s hopes to escape this place… It will have to stay there forever and ever… Are you proud of yourself?

Well, you should be! Because all the walls are now ready and we can finally move on to…

The Ball

Wow, you’ve made it this far!

You are ready… to add the ball to the game!

Same with the paddle, in order to add the ball all we have to do is drag and drop it from the Sprites folder into the scene.

And again we will have to add a Rigidbody 2D and a Box Collider 2D.

Now if we run the game again, something interesting is going to happen:

WHAT KIND OF WITCHCRAFT IS THIS?

Well, since the ball in a Dynamic object, gravity does affect it, in contrast to the paddle. But even though the paddle isn’t affected by gravity, it is still also a Dynamic object that can be affected by other objects, like the ball!

We still have to do something about it though because that’s not how Breakout works! Fortunately for us, that’s an easy fix! All we have to do is select the paddle, go to the Rigidbody 2D section on the inspector and under Constraints, select Freeze Position for the Y axis and Freeze Rotation for the Z axis.

Run the game now and the ball should stay on top of the paddle.

Well, unless you move it…

So that’s nice but again, that’s not how Breakout works! The ball should bounce off the paddle. In order to do that we have to create a Physics Material. Create a new folder named Physics Materials and once you open it, right-click, go to Create and click on Physics Material 2D. Name it Ball.

After you click the new Physics Material we added, on the Inspector, set the friction to 0 and the bounciness to 1.

 

Click on the ball and drag and drop the Ball Physics Material on the Materials property of the Rigidbody 2D.

 

Now if we run the game:

 

the ball will start bouncing off the paddle. That’s progress but still not the desired outcome. There are still a few things to be done for the ball to work correctly:

  1. The ball should move with the paddle and only start when we press the space key,
  2. Once we press the space key, a force should be applied to the ball so to the direction of the empty space on the top of the screen.

For the first one, on the Hierarchy, drag and drop the ball onto the paddle, making it a child of the paddle. Then, click on the ball and on the Inspector change the Body Type on the Ridigbody 2D section to Kinematic. Also, in the Constraints section, check Freeze Rotation Z.

Now the ball moves with the paddle. In order to trigger the ball to start moving on its own we’ll have to create a new script.

On the Scripts folder, let’s create a new script named BallController. Open it and change the source code to the following code. Save it and Add it to the ball.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallController : MonoBehaviour {

    private float force = 200f;
    private Rigidbody2D rigidbody = null;
    private bool started = false;

    // Use this for initialization
    void Start () {
        rigidbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update () {
        if(!started && Input.GetKeyUp(KeyCode.Space)) {
            transform.SetParent(null);
            rigidbody.bodyType = RigidbodyType2D.Dynamic;
            rigidbody.gravityScale = 0;
            rigidbody.AddForce(new Vector2(force, force));
            started = true;
        }
    }
}

Again, on Start() we are getting the Rigidboxy 2D object of the ball so we can use it later on. Boring stuff…

Update() is where most of the action is happening.

We check if the space key has been pressed and if the game has started. If one of the two is false, we do nothing. But if both are true, we are doing a couple of things:

  1. We set the balls parent to null letting it move freely to the scene,
  2. We set its body type to Dynamic and the gravity property to 0. This way it can be affected by other objects but not by gravity. Exactly what we need!
  3. We add a force to it and lastly,
  4. We setstarted to true so that we don’t enter this block of code again.

Now that’s more like it!

On the next part we are going to add a few blocks to break! Stay tuned!