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.
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.
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.
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.
Launch Unity and you’ll see the Project Wizard:
1. Click New project.
- Type a project name (anything you like).
- Choose a folder to save your project in.
- Select 2D as the game type (this changes a few settings to better suit to a 2D game).
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:
- 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.
- GameObjects – Unity’s main unit of game content – the player, the coins they collect, the enemies, etc.
- 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.
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.
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.
Here’s a screenshot of Unity in action:
Here’s what those different screen parts are:
- Hierarchy – a list GameObjects in the current scene. A triangle icon on the left shows a GameObject that has ‘children’.
- 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.
- Game – Displays the current scene as it will appear to the player. This is also where you can playtest your game.
- Inspector – Displays the components in the GameObject highlighted in the Hierarchy.
- Project – A folder containing everything in your game (GameObjects, levels, prefabs, sprites, scripts, etc.).
- 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.
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:
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:
- Right-click the GameObject and select Rename, or
- 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.
Add a Sprite Renderer
Click Add Component:
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.
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.
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.
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.
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.