Unity Tutorial


This tutorial provides an introduction to the popular and easy-to-use Unity game engine, which can compile for various platforms including PC, Mac, Smartphones, web browsers, consoles, etc. To get started, download the free version from Unity's page. You'll need to create an account during the installation process. You can sign up for a Unity Pro trial if you like, but this tutorial doesn't require any Pro features.
This tutorial was made using Unity 4.3

Creating a Project

Create a project and name it whatever you want. You'll be asked which packages you want to import. Choose the following:

  • char control
  • physics materials
  • standard assets
  • skyboxes
  • terrain assets
  • water (basic)

Note that you can import packages at any time by going to Assets -> Import Package

Go to File -> Save Scene as to give your scene a name. In Unity, scenes are basically levels, while projects are collections of scenes and assets (along with settings for the whole project, like controller settings, target platform, etc).

In the drop-down at the top right that says "Layout," select "wide".

Making Terrain + Navigating in 3D!

The top-left window is where you explore your scene. Right now there's nothing to explore, so go to GameObject -> Create Other -> Terrain to add some ground to our world. In the bottom-left window called "Hierarchy", "Terrain" should pop up. The Hierarchy window contains all the objects in our scene. The right window, called "Inspector", shows information about the object you have selected and lets you edit it. Under "Terrain (Script)" click the big gear button, and change the terrain's width and height to 400 meters by 400 meters (all units of distance in Unity are meters).
Hit landscape button to turn on grid.
Can use rightclick+wasd to move (shift faster), scroll zoom, middle click pan

Try the following:

  • Double-click the Terrain in the Hierarchy view to center it in the camera
  • Right-click to rotate your view
  • Middle-click to pan
  • Scroll to zoom
  • While holding right click, use wasd to fly around. Hold shift to go faster. Press Q and E to go up and down.
  • Click the dooda in the top-right to align your view with different axes.

Making and moving the player

The bottom-middle window shows the assets in your project. Assets include basically everything in your project, including models, textures, sounds, scripts, and "pre-fabricated" GameObjects (more on the last later). These are all stored in the Assets folder of your project, but it's important to never move or rename files from your file explorer; always do it through Unity. This is because Unity creates meta-data about each file, and outside changes will break the metadata.

We'll use one of Unity's default pre-made GameObjects for our player. Go to Standard Assets / Character Controllers / First Person Controller and drag it into your scene. Let's rename it "Player". Press the big Play button in the top-center to play your game!

Did you fall through the ground? That's because your character's collider isn't above the ground. To fix it, press the second button from the left in the top-left corner, that looks like a '+' with arrows. This tool lets us move gameobjects. The other 2 tools let us rotate and scale them. Then grab the green arrow and drag upwards. Play your game now and you can walk around.

Try the following:

  • Press Q, W, E, and R to switch between tools.
  • Hold control to snap
  • Hold shift+control while moving objects to snap them to a surface. This is a good way to align things with the ground.
  • Hold v to grab a vertex on an object and snap it to nearby vertices. (We can't actually do this yet because terrain has no vertices).
  • Move your camera elsewhere, select the Player in your hierarchy, and press ctrl+alt+f to move the Player in front of your camera.
  • Do the same but press ctrl+shift+f. This moves the selected object to wherever your camera is, rotated however your camera is.
  • Multi-select both the terrain and player in the Hierarchy. You can move, rotate, and scale them together!

Making pretty terrain

Let's spruce up our environment. Select your terrain again and choose the tool in the inspector that looks like a mountain with an up arrow. Use this to paint mountains on your terrain. The one to the right is the height painter: it paints areas to a specified height. To the right is the smoother, which smooths bumpy terrain.

Next is the texture painter. Before we can paint on a texture, we need to add one. In the Textures area click Edit Textures -> Add Texture. Click Select on the popup, and type "grass" to just look at our project's grass assets. Choose one. (Alternatively, you could drag the texture from the Asset window onto the little square). By default, the entire terrain will be covered with the first texture you choose. Let's add another rock texture for fun.

After that's the tree brush, which lets us paint trees onto our environment. You need to add a tree to paint like how you needed to add a texture before, so add a palm tree (the only one). When far away from the player, Unity automatically turns the trees into flat billboards.

The detail painter lets us paint other 3D things onto our landscape, like tall grass or little rocks. Add "Grass" as a grass texture. Similarly to trees, these aren't fully rendered when the player's far away.

Try the following:

  • Press shift+Q/W/E/R/T
  • Press , and . to switch between brushes
  • Press < and > to switch which texture/tree/detail you're painting
  • Press F to move your view over your paintbrush

Skyboxes and lighting

We need a sky! And some light!
Edit -> Render settings -> Skybox Material -> Choose one.

Skyboxes don't generate light though! Need a "default" light that will be everywhere.
Edit -> Render settings -> Ambient light.

To get a sun that casts shadows, we need a directional light.
Gameobject -> Create other -> Directional light.

  • Position of light doesn't matter, it comes from everywhere. Only rotation is important.

Don't stop playing, go to render settings, drag up fog density, toggle fog. You're now playing in Burnaby.
Gameobject -> Create other -> Create windzone. It's now windy! This also depends on the "bend factor" you defined when adding your trees.


  • So what is a GameObject, exactly? It's a container. Nothing but a container for Components with an ID number. When we go to GameObject -> Create Empty, we get a GameObject with a single component: the Transform component. Every GameObject must have a transform! Components are what give different objects different behaviours. First we'll play with some of Unity's built-in components, then we'll make our own.

Time to explore some simple gameobjects!
Gameobject -> make sphere
GameObject -> Move to view to move it to you. (or ctrl+alt+f)
The basic pre-made sphere comes with a number of basic components: the Transform; the Mesh Filter that contains its polygonal model; a Sphere Collider that lets it collide with things; and a Mesh Renderer that draws textures on the mesh. Most of them can be activated and deactivated with the check box next to their names. For example, deactivating the Mesh Renderer makes the sphere invisible.
To go to a component's documentation, click the blue book to the right of the component's name

Right now our sphere just floats. Let's give it some physics so it can bounce around.
Go component -> physics -> rigidbody.
It will now fall down! But dese balls ain't fun to play with yet.
Check Sphere Collider, give it a material, bouncy. You can bounce the ball!


So what if we want to save a "recipe" for a GameObject that has specific components with specific settings? Then we'll make a prefab. Making one is super-easy: just drag our Sphere from the Hierarchy into the asset window. Bam! It's now a prefab. The sphere's name will turn blue to indicate this.

Now make another Sphere by dragging the prefab from the asset window into your scene. The copies in your scene and the original blueprint are linked together. When you change a property in a copy, the property will be bolded. Clicking "apply" on that copy will apply those changes to the blueprint and to all other copies. You can also click "revert" to revert the copy to the blueprint.

Making a gun via parenting

The only thing more fun than bouncy balls is a gun that shoots bouncy balls. Let's make the physical gun first.

  • Create a cylinder. Rotate it 90*. (Or move the camera inside the player, look in the camera's direction, select the cylinder, and press ctrl+shift+f). Selecting the player's Camera gives shows a little "Camera Preview" in the scene, so you can tell if the gun's positioned right.
  • Uncheck or remove its Capsule Collider, so you're not constantly colliding with your gun.

We want our gun to follow the player around and look where they look. We've actually already got something that follows the Player around: the camera! Expand Player in the hierarchy view and you'll see there's a "Main Camera" attached to the player. (Cameras render the game to the screen from a certain position/perspective). Since the camera is attached/parented to the Player, the camera's Transform is relative to the player. That means it moves when the Player moves, rotates when the Player does, and scales with it too.
Deactivating a parent also deactivates all its children

  • We want the gun to move and rotate with the player, so parent it to the Main Camera (it's the camera, not the player, that rotates up and down).
    • Why did we make the gun a gameobject instead of just adding components to our player? This way lets us detach our gun from the player or easily add multiple guns everywhere (we'll use this later to make a stationary turret).

Making a Shooter component

Let's give our gun the ability to make and shoot bouncy balls. We'll do this by creating a "Shooter" component that allows the object it's attached to to fire some other object when a button is pressed.

  • Right click “Standard Assets,” create C# script. Name it “Shooter”.
  • Open it, should open in MonoDevelop.
  • Lookit that! Child of “MonoBehaviour.” MonoBehaviour is the class that basically all of your components will inherit from. It has some very useful functions.
    • The Start() function is called once when the object is created.
    • The Update() function is called once every frame.

Add these variables to our Shooter:

public int shootSpeed
public string triggerButton
pubic RigidBody bullet

* Why is our bullet a RigidBody instead of a GameObject? We could make it a GameObject, but by writing RigidBody instead, Unity goes ahead and interprets it as "Any GameObject with a RigidBody component on it." It's useful because it's more restrictive, and also because it gives us quicker access to the variables and functions of RigidBody. Usually instead of making something type GameObject we'll just make it type Transform or whatever we want to use.
* Save, attach to gun. Behold the inspector! We can now enter values for all those fields right in the Inspector, even in the middle of gameplay (though mid-game changes aren't saved). This makes prototyping in Unity very quick. Note that only public variables can be changed in the inspector, unless you're in Debug mode.

Triggering events from input

  • Now to make it do stuff! In Update(), add
    Debug.Log(“You pressed fire!”); 

* In editor, write “Fire1” as triggerButton.
* The Input class is convenient way to get custom inputs. By default, the “Fire1” virtual button mapped to left control key and left-click. Unity can show a pre-made dialog at game start to let people configure inputs.
* The Debug class is useful for printing messages, errors, or drawing stuff onscreen easily.

  • Maximize the debug window by clicking the bottom of the screen, hit play (may need to collapse messages). You should see a message when you click.

Creating bullets

*Let's shoot the spheres we already made as bullets. Need to Instantiate into the scene. After the Debug message, add:

Rigidbody bulletClone = (Rigidbody) Instantiate (bullet, transform.position, transform.rotation);
bulletClone.velocity = transform.parent.forward * shootSpeed;

* Need to cast it!
* We made our bullet of type RigidBody since it needs to move. Any object with a RigidBody works!
* Using transform.parent.forward instead of transform.forward since we rotated our gun.
* Instantiate makes a clone of an existing gameobject!

(Optional) The "DestroyAfterTimeout" script

Button prompts

Next we'll make a turret that you can turn on and off with a switch. First, place one of our gun prefabs in the scene.
Next, we'll create a "prompt zone" which will display a prompt message when we get near the switch.

  • Create a box, and set istrigger to true.
  • Give your player the tag "Player"
  • Create a new C# script called "PromptZone"
  • Attach this script to your cube, and set all the necessary variables (use Fire2 for action). When you enter the zone, the prompt should display on the screen.


How can we make many different types of components, all of which can be triggered by prompt zones?
Answer: we'll make a class called "Interactible" with an empty Interact() method. Then we'll have subclasses that override that Interact() method to do different things!

Our Interactible script (minus the auto-generated Start() and Update() methods) will just look like this:

public abstract class Interactible : MonoBehaviour {
    public abstract void Interact () ;

That's pretty short! This is an abstract class, meaning we're not actually allowed to use it. Try attaching it to a game object and Unity won't let you. You can only use it via child classes that inherit from it. The empty method there is basically saying "Any class that is an Interactible must have the Interact() method.

Let's make an Interactible class to activate and deactivate our gun: we'll call it GunSwitch.
Here it is:

Pretty simple, right? Note the override keyword: whenever we override a method from a parent, that needs to be there! Also note the Start() and Update() methods. Those methods belong to the MonoBehaviour class. Since an Interactible is a MonoBehaviour, and a GunSwitch is a Interactible, a GunSwitch is a MonoBehaviour, and has all its methods!

Animating a gun button

Let's say that, when we toggle our gun, we want a button sticking out of it to get visibly pushed. Let's make the button as a small cylinder sticking out of the top of the gun.

So if we want the button to simply move down, then up, when we interact, how would we do that? One option is to write a script with some variables: timeToMoveUp, timeToMoveDown, distanceToMoveUp, and distanceToMoveDown. Then, every time the Update() function is called, we look at these variables and move the button a bit.

I think you can see that that's a lot of work for something simple. Fortunately, Unity has a built-in animation system, that lets us gradually change an object's properties over time. Anything you can set in the Inspector, you can animate: position, rotation, size, velocity, gravity, etc. It's a powerful tool that will save you a lot of time!
Note: these animations are different from the traditional animations you can make in programs like Blender, which focus on manipulating polygonal models.

Let's check it out. Go to Window -> Animation. Select your button, and click the red "Record" button in the top-left of the animation window. You'll be prompted for a name for the animation you're creating. Choose a name. Then grab the vertical movement arrow for the button and wiggle it. Some diamonds should show up on the timeline. Each diamond is a "keyframe." You create animations by giving properties different values at different keyframes.

When the red button is pushed, you can grab objects in the scene and change their keyframe values that way. Let's move the vertical red line in our timeline to the 0:30 mark (half a second), then click the button that looks like a diamond with a plus to create a keyframe. Then, we can grab our button and lower it into the gun. You'll be able to see the value changing in the animation window. Create another keyframe at 1:00 and move the button back to its original position.

Play the animation. The button should move up and down smoothly. Add the animation to your button by selecting it and going Component -> Miscellaneous -> Animation, then drag the animation asset onto the Button's Animation component.

If you play the game, you should see your button moving up and down. We only want it to move when we trigger it, so uncheck "Play Automatically" in the animation component.
Add this field to our GunSwitch script:

public Animation animation;

Then add this line to the Interact() method:

Play the game. When you toggle your gun, the animation should play… and keep playing. To fix it, change the "Wrap Mode" of the Animation component to "Ping Pong". (If you can't see wrap mode, try switching to Debug in the top-right corner).

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License