top of page

Intro To Unity

In this unit, you will create a simple Flappy Bird kind of game to learn how to use Unity. Some of these lessons can be used for your final game (eg: creating menu systems). You will be responsible for writing the code for the player game object; all other code is provided.


1. Setup Unity

In this lesson, you will setup Unity and download the TappyPlane Artwork  containing the basic resources. Staying organized in Unity is extremely important. Follow this lesson carefully!
You need to install Unity version 2018.4.35f1 for this tutorial. Find it and install it in Unity Hub. When you open up Unity, this should be the first window you see. Choose Create New Project and save it a folder called Tappy Plane. Make sure the project is 2D!


2. Organize Your Project

Navigate over to the Project window at the bottom and right click to create the folder structure you need to organize your files. Add six folders called “Prefabs”, “Scenes”, “Scripts”, and “Graphics”, "Sound", and "Fonts".

  • Prefabs: In Unity, a Prefab is an object that can be reused and created such as bullets, enemies, or walls.

  • Scenes: A scene is like a level of a game.

  • Scripts: This folder will hold all the code.

  • Graphics: All the images used in the game. In 2D gaming, these images are called “Sprites”.

  • Sound: game sound for the project

  • Fonts: If you want to use anything other than the default Arial font, you need to put them in the project.


3. Bring in the Artwork

​Decompress the TappyPlane Artwork and drag in some graphics into your graphics folder. You'll need a plane graphic, rock up and rock down, and the background:


4. Setup the Background

Drag the Background from your Graphics folder into the SampleScene Hierarchy (under the main camera). Scale up the background to 2.5 on the X and Y axis.


5. Add The Player

Drag and drop the plane into the Hierarchy. Scale the plane on X and Y for 2.0. Click on the Sorting Layer and make a Background sorting layer and a Sprite sorting layer. Make sure your graphics have the right sorting layer, otherwise, you won't be able to see the plane in front of the background. Label the plane as "player". Click the Add Component at the bottom of the menu and add a RigidBody 2D to the plane. RigidBody adds Physics to our plane. Hit the play icon at the top to see what happens.


6. Controlling the Player

Let's get started on making the Player functional. We are going to program the player to jump when the space bar is hit. You are responsible for coding the player. Other code for this game will be provided. Your instructor will walk you through the process; however, you are welcome to see the finished script here.   Right click in your Scripts folder and choose C# Script. Name it Player with a capital P.

There are two main parts in this code: the jumpForce variable and the Update method.

  • jumpForce stores the force applied to the player when we jump. It is of the Vector2 type, meaning that it stores two values: x and y. Because this variable is public, you can change its value in the Inspector.

  • The Update method is a function that is called every frame of the game. In it, if the spacebar button is pressed, a force is added to the rigidbody.

Add this script to the Player object the same way you added the RigidBody 2D, with the Add Component button. Drag the player into the rb field of the player script in the Inspector (see image below).  Now when you press Play, you’ll be able to jump up and fall back down.


7. Creating the Obstacles

​Decompress the TappyPlane Artwork and drag in some graphics into your graphics folder. You'll need a plane graphic, rock up and rock down, and the background:


In the file menu, go to GameObject->Create Empty. This will add an object to the scene that is invisible and will serve as a folder that holds our rock obstacles. Name it “RockPair”. Reset it's transform by clicking on the gear icon in the Inspector and hitting reset. Drag the two rock GameObjects onto the RockPair object after resetting the Transform.​ 


8. Moving The Obstacle

Add a “RigidBody 2D” component to the RockPair parent object. In the inspector, check Is Kinematic. This prevents the obstacles from being affected by gravity. Download the Obstacles.cs Script, decompress it and double click on it to import it into your project. Open it up and comment each line of code showing that you know what it does. Move the rockpair to the right of the background and add the Obstacle script to it. Make sure you move the rockpair and not the child objects (rock and rockdown). In the script inspector, set the velocity to -4 on X and 0 on Y. Hit play and give it a try.


9. Generating Obstacles

We need to create new rock obstacles every few seconds. To begin, drag the RockPair Object into the Prefabs folder. This turns RockPair into a Prefab (it will turn a slight blue color in the Hierarchy), which is an object that can be created and destroyed many times. Delete the RockPair object that is in the scene. Download the Generate Script, decompress it, and import it into your project. Create an empty game Object, like we did in step 7, reset its transform, and name it Spawner. Drag the new Generate Script onto the Spawner GameObject. Drag the Rockpair prefab into the Generate Script Obstacles Field in the Inspector:


10. Multiple Obstacles

As an video lesson, you can create multiple prefabs ("types") of obstacles for your game.  You'll need to import the asset pack Multiple Obstacles. Then, follow the Multiple Obstacles tutorial.

11. Killing the Player

​​Time to get back to coding. You may have noticed that running into the obstacles doesn't do anything. Time to make your game dangerous! For this lesson, we will add colliders to both the player and the rocks and do a little more coding on our Player Script.

12. A Touch of Randomness

Creating some random elements in your game will make it more interesting and less predictable. Let's add some randomness by changing our Obstacle Code. Then you can make revisions in the inspector to add a certain amount of randomness to the game. You'll need to download the Randomness In Obstacles asset package and import it into your game. Play your game, notice how the rocks move up and down? Try changing the script numbers in the Inspector for your prefab. You'll need to drag your prefab back into the Hierarchy, make your changes and click apply at the top of the Inspector.

13. The Shredder

One issue with our current game version is that the obstacles continue on after the player has passed them. That means they stay in memory for ever! Eventually your computer will run out of memory and crash or the player lags out. Let's fix that. Create a new empty GameObject and call it Shredder. Remember to reset it transform right away. Add a Box Collider 2D to the Shredder using Add Component. Click the Edit Collider button and resize/move it to left of the background and make it taller (see image below).  Download the Shredder.cs script and drag it onto your Shredder GameObject. An error will appear at the bottom, we will fix that in the next section. You MUST edit your Rockpair Prefab to include a Box Collider 2D, set it to Trigger, and edit to to appear in one of the 2 rocks (see image).


14. Score Display

Let's display the player's score as the game progresses. Download the Score Scripts. The reason why you got an error when adding the Shredder Script is that it calls for the score to increase when an obstacle is passed by the player and enters the Shredder. This dependency is not the best way of coding but was left this way to keep things simple. Download fonts that you want to use in your game and copy them into the Assets/Fonts folder. Create a UI Text by selecting GameObject/UI/Text. This will automatically create a UI canvas in your project hierarchy (mandatory). 


UI automatically creates far larger than the Game space. Double click on Canvas and the window will zoom out to see the UI. To go back to the game screen, double click on Player or another GameObject. Under the Canvas, select the text and then click on the Anchor Pivot selector. Hold down the Shift and the Alt Key and select the Bottom Left Setting (see image). Adjust your text by using the Move Key and putting it further inside of the White Camera Rectangle area.


Customize your text. Make your font size larger (50ish) and change the font. Change the font to say Score. You will need to resize your text box to accommodate the new size (see image tool below). Rename your Text to Score Label and duplicate it by selecting it in the hierarchy and pressing Ctrl + D. Rename it as Score (capital S) and move it next to the Score Label. Put a zero in the Text field (see image below). Attach the script ScoreKeeper to the Score UI Object. The script ScoreDisplay is only used in the EndGame Screen. Make sure you added the Box Collider 2D in the Shredder section of this tutorial. Test your game, does the score work?


15. Things You Want To Hit

​In the example game, the player tries to avoid obstacles but hit stars that dramatically increase the score. You'll need the Stars Code and suitable artwork. Your artwork does NOT need to be stars! View the Hitting Stuff Tutorial to get started. You will need to update  your Player Code here.

16. Scrolling Background

Our player is suppose to be flying through an environment but our background is stationary making it look like it's just jumping straight up and down. In the optional Moving Background Tutorial, you can create the moving effect for your player. You will need the ScrollingBG Code for this lesson.

17. Game Menus

Most games include other game screens such as the Main Menu and the Game Over Screen. Download the Game Screens Package and import it into your Unity project. Then follow the Game Screen Tutorial.  To finish off the game screens, use Game Screen Tutorial Part 2. You'll need to edit your Player script to include the new Die Method.

18. Shooting Obstacles

For this tutorial, you will need to have completed #8 (Multiple Obstacles) and #14 (Stuff You Want to Hit).  Download the Shooting Obstacles Assets before you begin the video tutorial. To make your player shoot using the left control key follow the Projectile Tutorial A. As mentioned in the video, you will need to edit your player code. You can see the changes on Player Edit A and Player Edit B. To finish up, you will need to edit your shredder and obstacles - use Projectile Tutorial B.

bottom of page