unity3D tutorial

Unity3D Tutorial

First of all, download Unity, install it to your computer and launch it.
 
After the splash screen, you will be prompted with a window asking you whether to open an existing project (such as the Bootcamp demo provided with Unity itself) or to create a new one. Choose this second option and enter the path of the folder in which you want the project to be saved: 

You do not need to import any packages at the moment. Click on the "Create" button and the editor will open with a blank scene in it. You can think of a scene as a level in the game: each project contains at least one scene.

Save the current scene by clicking on File -> Save Scene. A new entry, marked with the Unity logo, will appear in the Project panel. Ensure to save your work frequently.

In the Hierarchy panel you will notice that a default camera has already been created: once you select it, its properties appear in the Inspector tab on the right.

Let us now start adding objects to the scene. We want to obtain a cube that moves on a floor: let us start with the latter. Our floor will be represented by a plane that can be created by going to GameObject -> Create other -> Plane. A Game Object is a basic entity in Unity that is used as a sort of abstract type for whatever item appears on the scene.

You can move around the scene with the following commands: 
  • the central button of the mouse allows panning
  • pressing Alt + left mouse button allows rotating 
  • the mouse wheel allows zooming
Select the plane and scale it by setting the relevant coefficients to 2 along x and z directions in theInspector tab. 

If you now hit "Play" (the first blue button on top of the editor", you will just see a dark scene with a grey trapezoid and a blue background color. In order to improve the situation, we can first of all add a light source. To do so, go once again to GameObject -> Create other -> Point light and a new light will be added to the scene. Adjust its y coordinate so as to properly position it with respect to the plane and enlarge its Range around 20, so as to allow the whole plane to be lit. The result should look like this: 

Let us now add what will be our "main character": a cube. As for all other primitives, it can be created from GameObject -> Create other -> Cube. 
Up to now, the scene is still static: it only features a cube standing on a plane. Before we introduce some more dynamism, we need to add a component to the cube, namely a Character controller, which will take care of applying physics to the cube and allow it to move in a realistic way as if it was a character in the game. To do so, select the cube and then go to Component -> Physics -> Character Controller. Unity will ask you if you really want to replace the existing collider with this one: this is because each primitive object already has a collider component, but the Character controller is somewhat more finely tunable for our purposes. Therefore, confirm your choice.

We will now introduce scripting to make the cube move around by pressing the keyboard arrows.
In the Project tab, create a folder called "Plugins": by default, this is the location where Unity looks for scripts. Select the folder, right-click on it and choose Create -> C Sharp script. Of course, you can also choose to write in JavaScript or even in Boo, since instructions are quite similar in all of the three languages. 
A new, empty script called NewBehaviourScript has now been created: in order to modify it, select it and click on the Edit button on top of the Inspector panel. This will open the script in an editor such as MonoDevelop, which is provided for free together with Unity and is similar to VisualStudio. Edit the code as follows: 

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
public float speed = 3.0f;
public float rotationSpeed = 3.0f;

// Use this for initialization
void Start ()
{
// no initialization required
}

// Update is called once per frame
void Update ()
{
//get character controller component
CharacterController cc = this.GetComponent<CharacterController> ();
// compute rotation amount
float y_rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
// rotate around y axis:
transform.Rotate (0, y_rotation, 0);
// compute translation amount
Vector3 forward = transform.TransformDirection (Vector3.forward);
float current_speed = Input.GetAxis ("Vertical") * speed;
// apply translation
cc.SimpleMove (forward * current_speed);
}
}

This will make the cube rotate around its y axis when left or right arrow keys are pressed and move forward or backward along the direction it is facing when the up or down arrows are pressed. 

Before the script can actually do anything, we need to attach it to the cube: this can be done by selecting the cube in the Hierarchy panel and dragging the script onto it or to the Inspector panel where the cube properties are shown:


Note that the public variables declared in the script are also visible and editable from the Inspector panel.

If you now hit "Play", you will be able to see the cube moving around according to key pressure. Note that as the cube falls over the borders of the plane, it will keep on falling indefinitely due to physics applied to it. 

However, the whole scene might look nicer if the camera would follow the cube rather than being still. To do so, we will use another script and attach it to the main camera. We will not write it ourselves, though: we will use one of the ready-made ones provided with Unity. Therefore, go to Assets -> Import package and navigate to the folder where you installed Unity: prefab packages can be found in Unity\Editor\Standard Packages. Now pick Scripts.unitypackage: a window will appear asking you to select which of the items contained in the package you want to import in your project. We only needSmoothFollow.js


Click on "Import" and the script will be now visible in the Project panel under Standard Assets -> Scripts -> Camera scripts. As we did before, drag it to the main camera. Now, where the script appears among the properties of the camera, we will need to specify the target that the camera is meant to follow, i.e. our cube. We can do so by dragging the cube from the Hierarchy panel to the target field of the script. If you like, you can also adjust the other properties of the script, such as distance from the target etc. Recall that these are public variables in the script itself.

If you now click play, the scene will look nicer: the camera will follow the cube as it moves around. 


The application is now complete. This is only a glimpse of what Unity can do, of course: however, the aim of this tutorial was to emphasize the ease with which nice results can be achieved with it, and how it will take care of many details letting your imagination free of spreading.
Comments