Simple Unity Game tutorial

Cubes game

In this tutorial I will teach you how to make a simple game. This tutorial is mainly aimed at users who are new to Unity. So let’s begin.

First off create a new project if you haven’t already made one.

Right-click inside your Project tab and click Import Package > Character Controller. Click on “none” at the bottom left and scroll down to the bottom. Under Scripts tick “MouseLook.cs”. This will import the MouseLook script. It will help us rotate the camera around using mouse movement.

Import package

Now that we’ve got the MouseLook script let’s attach it to our camera. Select the MainCamera in the Hierarchy and in the top menu go to Component > Camera-Control > MouseLook. Viola! Now if you play the game you can rotate the camera around by moving the mouse but our scene is empty and all you see is a blank blue background. Let’s design a simple level.

In the top menu go to GameObject > Create Other > Plane. In the Inspector set its x/y/z position to “0″ and set its x/y/z scale to “2″, so our camera can see it. Hit Play and now you can see that if you move the mouse the camera will rotate!

But its pretty dark in here, isn’t it? Let’s add a light so we can see things a little better.

Go to GameObject > Create Other > Point Light and use the following values:

Point Light

Play the game and you’ll see its not dark anymore.

So, now we’ve got a simple level and we can rotate the Camera using the mouse! But that’s not interesting. Let’s add some stuff we can interact with.

Go to GameObject > Create Other > Cube and in the Inspector set its x/y/z position to “X: 0, Y: 0.5, Z: 0″. Next go to Component > Physics > Rigidbody to make it a Rigidbody. Now it will be affected by gravity and it will respond to collisions with real time Physics etc. You can read more about Rigidbodies at the Unity Documentation.

Go to Edit > Duplicate and make 5 copies of it so you’ll have 6 cubes in your scene. Position them like this:

Cubes

Now we’re gonna make the player throw a ball. Go to GameObject > Create Other > Sphere. We want the ball to be affected by gravity and want it to collide with cubes. So add a Rigidbody to it by going to Component > Physics > Rigidbody. Position the ball right in front of the cubes but not too close the cubes.

To make the player throw the ball we’ll need a script. In the Project tab click on Create > Javascript, name it “ThrowScript”. Double click to edit the script and use this code:

// This variable will contain a refernce the ball
var ball : GameObject;

// Speed of the ball
var speed : float = 800f;

function Update ()
{
	// If the Space key is pressd, apply force on the ball in z-axis (forward)
	if(Input.GetKeyDown("space"))
		ball.rigidbody.AddForce(0, 0, speed);
}

We’re almost done. In the Project tab drag the ThrowScript and drop it over the MainCamera in the Hierarchy to attach it to the camera. All we need to do now is to tell the script which ball to use. Select the MainCamera and scroll down to the ThrowScript in the Inspector and drag and drop the Sphere into the Ball variable.

That’s it! Our simple game is now finished. Play the game and press space and you’ll see how the cubes react when the balls collides with them.

Cubes game

If you have any questions or tutorial requests post a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>