UPDATED FOR UNITY 5
PrerequisitesIf you don’t already have Unity, download a free copy right now: http://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.
CompetencyI 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)
DownloadsIf you want to try out the final product, check out my Completed Pong Clone. This is what you’ll have created by the end of Part 10. You can also download the completed project here: Pong Unity Project, which you can open in Unity and look around in. 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 ReadyOnce 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.
Unity Game StructureA 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.
PrefabsA 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/childrenA 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 OverviewYour 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:
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 BallNow 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 GameObjectOne 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 GameObjectClick 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.
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 SpriteIn 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 RendererClick 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 RendererHere 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 PresenceOur 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 ColliderThis 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: 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:
RecapNow 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.