Unity3D is a platform for creating real-time, interactive 3D (I could have just said games, but I wanted to be more general). Not only is it easy to use and learn, but can be exported for practically every platform under the sun (iPhone, Android, PC, Mac and even an browser web player). Although I have only made a simple tutorial, I gather from the range of projects out there, that it is just as robust as any development program.
Brick Blaster
The little “game” I want to show you how to make is basically shooting balls at a wall of blocks. A simple concept, but potentially the basis for an entire game. What we will need is a floor, a camera, a light, a wall made of separate blocks and our balls to shoot at the wall. The balls and the wall are going to be created with code, but the rest of the elements will be setup in Unity’s 3D editing environment. Creating the elements is pretty straight forward. The floor is created from the menu with GameObject > Create Other > Plane. I have also created an empty object called SpawnPoint to attach our wall to.

Unity3D scene from top view
In order to create game objects from code, they need to exist in the library as “Prefabs”. This is how to create a prefab, directly from the site.
- Choose GameObject->Create Other->Cube
- Choose Component->Physics->Rigidbody
- Choose Assets->Create Prefab
- In the Project View, change the name of your new Prefab to “Brick”
- Drag the cube you created in the Hierarchy onto the “Brick” Prefab in the Project View
- With the Prefab created, you can safely delete the Cube from the Hierarchy (Delete on Windows, Command-Backspace on Mac)

Unity3D project view of all assets
With these prefabs in our library, we can now “Instantiate” those objects dynamically in the games runtime. For this game we want to achieve 3 different functionalities, and we will have 3 scripts for that. One script will build the wall, and will be attached to the empty SpawnPoint object. Another will control the aim of the camera with the arrow keys, and the final will control the firing of the balls from the point of view of the camera. Now lets take a look at our 3 scripts. All three are in JavaScript, but you can code in a few different languages, and even chop and change between languages within Unity. I will show the scripts first, with code comments, and then I have a few notes for the end.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| //To be attached to the Main Camera
var moveSpeed : int; //The speed of camera movement
//The Update function is run every step or frame of the game
function Update () {
//The following 4 if statements test if the arrow keys are being
//pressed and rotate the camera in the right direction
if (Input.GetAxis("Horizontal") > 0) {
//In unity, transform refers to the object to which the script
//attached. Essentially like referring to "this" in javascript
transform.Rotate(Vector3.up * Time.deltaTime * moveSpeed);
}
if (Input.GetAxis("Horizontal") < 0) {
transform.Rotate(Vector3.down * Time.deltaTime * moveSpeed);
}
if (Input.GetAxis("Vertical") > 0) {
transform.Rotate(Vector3.left * Time.deltaTime * moveSpeed);
}
if (Input.GetAxis("Vertical") < 0) {
transform.Rotate(Vector3.right * Time.deltaTime * moveSpeed);
}
} |
The axis mentioned above is basically an input and is defined in Edit > Project Settings > Input. Unity has preset most inputs that you would want, and is quite clever about how they are setup. Each input has backup keys, sensitivities and all sorts of other stuff that I don’t understand yet. When they say Axis though, it should be understood that it includes a control that works in two different directions. For example, the left and right keys (and as a backup the A and D keys) are represented by the “Horizontal” axis. To test if the Horizontal is going left or right, we test if it is positive or negative.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| var bulletPrefab : Transform; //The bullet object
var shootForce = 10.0; //The force of the bullet
//Our function to instantiate and fire a ball
function FireRocket () {
//Instantiate the object at the cameras position
var instanceBullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
//Add a force to the ball in the direction the camera is facing
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
// Calls the fire method when holding down ctrl or mouse
function Update () {
//Test if the fire1 button has been pressed
if (Input.GetButtonDown("Fire1")) {
FireRocket();
}
} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| //Attached to the SpawnPoint empty object
//This is left variable and is defined in Unity's user interface
var prefab : GameObject;
var gridX = 5; //How many columns
var gridZ = 5; //How many rows
var spacing = 3; // Space between rows and columns
//The Start function is called when the object enters the scene
function Start () {
for (var z = 0; z < gridZ; z++) {
for (var x=0;x<gridX;x++) {
//Create a position variable based on the position of SpawnPoint
//and the position in the grid
var pos = transform.position + (Vector3 (x, z, 0) * spacing);
//Instantiate a clone of the chosen prefab object
Instantiate(prefab, pos, Quaternion.identity);
}
}
} |
Ok, well that looks like it. I nice simple tutorial dealing with a few basic concepts of Unity3D. There is obviously a tonne of other stuff to look at, for instance, the physics is very basic at the moment (the cannon balls don’t even bounce) and there are no textures, but I am sure there will be other tutorials.
The final product. Hitting the scales at less than 50kb. Arrow keys to aim, and mouse 1 or left ctrl to shoot.