Beginner’s Guide: Create a Pong Clone in Unity: Part 1

Do you want to create a game in Unity, but don’t know where to start? This guide is for you. In simple steps, explaining everything as I go, I’ll show you how to use Unity to build a 2D Pong clone.

 

CREATED FOR UNITY 5. This tutorial series was originally created for Unity 5, which is a few years old now. Most of the content is still correct/relevant, but a few things might not work as expected. Try downloading the full project from the link below to test if it works on your version of Unity.

Introduction

Prerequisites

Download Unity
If you don’t already have Unity, download a free copy right now: https://unity3d.com/unity/download. This guide is updated for Unity 5 (free version).

You need a code editor for the scripting. Unity provides one called MonoDevelop, which you will be prompted to install when you install Unity. You can use a different editor if you want (e.g. Visual Studio), as we’ll just use it to edit a couple of small scripts.

Competency

I will assume you have:

  • No Unity experience
  • At least fundamental coding knowledge (though you can use the code I provide without understanding it fully, and I’ll explain how the small amount of code in this project works)

If anything doesn’t make sense or is not explained enough, please use the comments at the bottom of this guide to ask a question.

Note: Once I’ve demonstrated a particular technique or skill I will not repeat it in detail again. Refer back to earlier parts of this tutorial if you can’t remember all the steps (and I wouldn’t expect you to remember them).
Note to Mac users: everything here will work for you, but note that occasionally I’ll mention Windows-specific keyboard shortcuts, and the screenshots might look a little different.

Downloads

Download the Unity Project for this Game
You can download the completed project here: Pong Unity Project, which you can open in Unity and look around in.
Download Assets Required for this Project

You’ll need a few assets to build this game, so download the assets file before you begin, and keep it handy:

Pong Assets zip file

Getting Ready

Once you have Unity installed (along with MonoDevelop or another code editor of your choice – MonoDevelop comes free with Unity), you’re ready to start.

  1. Launch Unity and you’ll see the Project Wizard.
  2. Click New project.
  3. Type a project name (anything you like).
  4. Choose a folder to save your project in.
  5. Select 2D as the game type (this changes a few settings to better suit to a 2D game).
  6. Click Create project.

You’ve now launched Unity with a brand new project!

Unity Game Structure

A game is made up of various things – graphics, audio, levels, code scripts, etc. In Unity these things are grouped roughly into the following:

  1. Scenes – Each scene is a ‘screen’ in your game. Your main menu can be a scene, each level in your game could be a scene. If your player clicks the ‘Menu’ button you can launch the menu screen.
  2. GameObjects – Unity’s main unit of game content – the player, the coins they collect, the enemies, etc.
  3. Components – The raw content of your game – sprites, scripts, audio, etc. Components are usually combined together within a ‘parent’ GameObject. For example your main player GameObject might have sprites, animations, and behaviour scripts components.

Prefabs

A prefab is a GameObject (usually with several components, often with multiple child GameObjects) that acts as a template. For example, a Space Invaders game might have 50 invaders on the screen, but they are not all created individually – a prefab lets you create a prototype enemy, and then create variations with a few modifications, like different graphics or different values for hit points or speed.

You may also hear of asset packages, which are packages of GameObjects, components, and prefabs, which you can download from the Unity Asset Store.

Parents/children

A bit of jargon I should explain right away is the parent/child relationship. When I refer to a child, I mean an item that is within another item in the overall hierarchy. For example a GameObject called ‘Enemies’ might contain 20 bad guys. Those bad guys are the children of the Enemies GameObject. Importantly, whatever you do to the parent can affect the children. If I move the Enemies parent object to the left, all of the bad guys will move to the left. Using this parent/child relationship effectively can make your games simpler and more manageable.

Unity Screen Overview

Your fresh Unity screen will have several empty panels and windows (because there is nothing in your game yet!). Before we get started let’s find our bearings.

Unity’s layout is completely customisable, so you can move the different parts around to suit your own tastes. Your screen will therefore be laid out slightly differently to mine.

Here’s a screenshot of Unity in action:

An example of how the Unity screen looks while developing a game

Here’s what those different screen parts are:

  1. Hierarchy – a list GameObjects in the current scene. A triangle icon on the left shows a GameObject that has ‘children’.
  2. Scene – Shows everything in your scene regardless of what is actually visible to the player. This is where you move things around to build your scene visually.
  3. Game – Displays the current scene as it will appear to the player. This is also where you can playtest your game.
  4. Inspector – Displays the components in the GameObject highlighted in the Hierarchy.
  5. Project – A folder containing everything in your game (GameObjects, levels, prefabs, sprites, scripts, etc.).
  6. This area shows the files in the folder highlighted in (e). You can double-click files in here to open them.

Create a Ball

Now that we have a basic understanding of Unity, let’s start on the fun stuff. We’ll learn more about how Unity works as we go.

Unity created a blank scene when you created your project, and by default a new scene has only one GameObject called MainCamera. The camera represents the player’s eyes. If you don’t have a camera your player won’t see anything!

Create a GameObject

One of the most common actions you’ll do in Unity is create an empty GameObject, so that’s the first thing we’ll do.

From the menu bar (at the top of the screen) choose GameObject | Create Empty:

Creating a new, empty game object

You’ll notice a new item in Hierarchy called GameObject. Let’s rename that.

Rename a GameObject

Click on the new GameObject to select it. Use either of the following ways to rename it:

  1. Right-click the GameObject and select Rename, or
  2. Press F2 on the keyboard.

Then type a new name – call it Ball – and press Enter. Hierarchy should now look like this:

While Unity is very easy to use, creating a ball isn’t as simple as changing an empty GameObject’s name! We need to add some components to that empty GameObject to turn it into a ball.

Add a Sprite

In the assets that you downloaded earlier, find the ball image in the Sprites subfolder. To add the image to your project, simply drag-and-drop the file into the Project panel in Unity. You want the image in the root Assets folder.

Select Ball in Hierarchy, then look over to the Inspector. Note that the GameObject’s name is shown at the top of Inspector.

An empty GameObject is not actually empty because it always has a Transform component, which is used to move it around, change its size, etc.

Add a Sprite Renderer

Click Add Component:

Select Rendering:

And finally Sprite Renderer:

You’ve added a Sprite Renderer component to the Ball GameObject. Sprite Renderers display a sprite, but we need to tell it what sprite to display.

Add the Sprite to the Sprite Renderer

Here is what the ball’s Inspector pane looks like now:

See that highlighted area in the Sprite field where it says None (Sprite)? That means a field/variable that the Sprite Renderer uses is currently empty.

Inspector Fields: When you see fields in the inspector you can change their values. If a value is supposed to be a number you can type in a number; if it’s a bool (true/false) you can tick/untick a box. When a component is required you’ll see the component type in the parentheses (e.g. Sprite).

To populate this value in the Inspector you simply drag-and-drop the component you want into that space:

Once you’ve succeeded in that your Inspector should look like this:

Now you’ll see the ball in the Game and Scene windows.

Zoom around: You may need to zoom in the Scene window to see objects clearly. You zoom with the mouse scroll wheel, and you pan around by holding the right mouse button and moving the mouse.

A Physical Presence

Our ball is currently just a round image. We need to give it some physical presence so that it can bounce off things. In Unity we use colliders to give our GameObjects their own space.

Collider: a shape that defines an object’s physical presence. Unity can detect when two colliders collide, and this helps automate physics interactions.

Because our game is 2D, our ball is actually a circle, not a sphere, so we’ll add a circle collider.

Add a Circle 2D Collider

This is done the same way we added the sprite, so start by clicking the Add Component button in Inspector (make sure you have the Ball GameObject selected in Hierarchy).

Select Physics 2D:

Then Circle Collider 2D:

Unity cleverly matches the collider shape to fit our sprite by default, and this works perfectly for our needs. If you look closely in the scene view (zoom if you need to) you will see a green circle matching the shape of our ball – this is the collider. When another collider touches that green circle Unity knows about it.

Our Ball GameObject now looks like this in the Inspector:

The ball doesn’t currently do anything…we’ll work on that later.

Now let’s save our scene and project.

Go to File | Save Scene. You’ll be prompted for a filename. Let’s call it ‘Scene1’. Save it in the default folder that Unity suggests (this will be the root Assets folder of your project).

Then go to File| Save Project.

Recap

Now you’ve had a quick intro to some of the main concepts of Unity game building, let’s take a short break and recap what we’ve just done.

  • We looked at an overview of the Unity screen layout – Hierarchy, Inspector, Game and Scene windows, and the Project folder, and by now you have some idea of how they are used and what for.
  • You created your first empty GameObject and learned that a GameObject is a container for components, which can be sprites, colliders, etc.
  • You built up a ball GameObject by adding a sprite and a collider to give it a physical appearance and shape, using the Add Component button as well as dragging-and-dropping a component into an Inspector field.

As I mentioned in the introduction, I won’t be repeating the above detail, so refer back to this section any time you need a refresher on these fundamentals. When I say later on ‘add a sprite renderer to the GameObject’, I’ll assume you either know how to do this or will remember to come back to this section to remind yourself.

In the next part we’ll build on the above basics a bit, and start laying the foundations for Pong.

Read Part 2.