Gigi Labs

Please follow Gigi Labs for the latest articles.

Monday, June 10, 2013

Unity3D: Arkanoid

Hullo! :)

One of the subscribers of the Programmer's Ranch Facebook page requested an article on creating Arkanoid with Unity. So... here you go. :)

Create a new Unity project and save your scene. Set up your scene using scaled up cubes for the scene boundaries (except for the bottom), and put in another cube for the paddle and a sphere for the ball:



Make sure that all objects have the same z-coordinate for position, and that they are visible to the camera.

The game mechanics for Arkanoid are very similar to those of Pong, so we will be following very similar steps. As a matter of fact, we can use the same Ball script we used in Pong. Create a new C# script, attach it to the sphere, and after opening it in MonoDevelop, paste the code we used in Pong:

public class Ball : MonoBehaviour
{
    private Vector3 direction;
    private float speed;
 
    // Use this for initialization
    void Start ()
    {
        this.direction = new Vector3(1.0f, 1.0f).normalized;
        this.speed = 0.1f;
    }
 
    // Update is called once per frame
    void Update ()
    {
        this.transform.position += direction * speed;
    }
}

Attach a Rigidbody to the sphere via Component menu -> Physics -> Rigidbody, which will allow the ball to bounce off surfaces. Remember to turn off "Use Gravity".

Next, select the Main Camera and change Projection from Perspective to Orthographic to remove perspective and end up with a more suitable 2D view. Change the Size property until the scene fits the camera's view properly:


Create a new C# script and name it Paddle. Attach it to the cube to be used as a paddle. Paste the following code to enable left and right movement via arrow keys:

public class Paddle : MonoBehaviour
{
    private float speed = 0.1f;
 
    // Use this for initialization
    void Start ()
    {
 
    }
 
    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
            this.transform.position += Vector3.left * speed;
        if (Input.GetKey(KeyCode.RightArrow))
            this.transform.position += Vector3.right * speed;
    }
}

This is quite similar to what we did in the Pong article, except we're moving left and right instead of up and down. We are using the static vectors Vector3.left and Vector3.right, which are really just shortcuts for unit vectors along the particular directional axis (in this case (-1, 0, 0) and (1, 0, 0), respectively). We similarly used Vector3.up ((0, 1, 0)) and Vector3.down ((0, -1, 0)) in Pong.

We can now take care of the bricks. Since we will have a lot of them, it's best to create a prefab. From the GameObject menu, select Create Other -> Cube, and drag it into the Project panel in Unity to make a prefab out of it. Rename it to Brick. Create a new tag called Brick and tag your brick as such. Remember to click the Apply button in the inspector to make the tag apply to all instances of the prefab.

Proceed to duplicate (Ctrl+D) the brick so that there are many bricks in the top part of the screen:


To make the ball destroy bricks as it hits them, add the following code at the end of the OnCollisionEnter() method in the Ball script:

        if (collision.gameObject.tag == "Brick")
            GameObject.Destroy(collision.gameObject);

You can now play happily as the ball destroys bricks but not the paddle or the edges:


That wraps up the basic game mechanics for Arkanoid. As an exercise, add victory (for when you clear all the bricks) and defeat (for when the ball escapes below the paddle) conditions.

In these recent tutorials about classic arcade games, I have purposely focused exclusively on game mechanics (e.g. bouncing, shooting, etc). This was intentional. When making a game, you should first concentrate on making solid game mechanics that work perfectly. Then, you can focus on building the rest of the game (levels, menus, screens between levels, etc) and polishing (colours, animation, etc).

In fact, this Arkanoid game looks like shit. The first thing I'd do after finishing the game mechanics is give it a touch of colour (via materials, and put in a point light to brighten the colours):


...and here's what it looks like in-game:


This colouring took a couple of minutes to do, and I think you'll agree with me when I say that the game looks much more alive. Making the game vibrant and fun is just as important as programming the game mechanics, so don't overlook it!

Thanks for reading, and I hope you found this useful! :)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.