User Inputs :: GameDev Unity3D cookbook

User Inputs are the commands users use to control characters in a game. So, user inputs play a main role in any game development.

User Input ::
You should create a new JavaScript in your Project panel, and name it BasicInputHandler, and insert the following code:
[code lang=”Javascript”]
function Update () {
//Move object through Z axis
if(Input.GetAxis(“Vertical”) > 0){
transform.Translate(Vector3.forward * Time.deltaTime);
}
else if(Input.GetAxis(“Vertical”) < 0){
transform.Translate(- Vector3.forward * Time.deltaTime);
}

//Move object through X axis
if(Input.GetKey("d")){
transform.Translate(Vector3.right * Time.deltaTime);
}
else if(Input.GetKey("a")){
transform.Translate(- Vector3.right * Time.deltaTime);
}
}
[/code]
Add a cube in your scene, drag and drop, the BasicInputHandler script to this cube.

This script check the user input in two ways: through the Vertical axis in Input object, and if keys were pressed.

Veja o exemplo aqui.

Axis option ::
In Unity3D, there is this Input global object, that captures any user input, and make this information available to all other game objects. This Input object has some default settings, like Vertical and Horizontal axis. The Vertical axis is activated by W, S, up-arrow, and down-arrow.
THOSE SETTINGS CAN BE MODIFIED at EDIT >> Project Settings >> Input menus. The Inspector panel will show the axis configurations in the Input object, and you can modify them as you wish.

Keys option
You can also use the GetKey function to capture user inputs. The Input object, captures when keys a pressed, and this information is available to you.

Both ways are valid, and easy to use, but they slightly differ in the results.
Check the sample, and pay attention the at the difference between the controls W/S, and A/D, when you release the keys.

Update :: GameDev Unity3D cookbook

“Update” is a function used in Unity3D, which refer to: “The need to provide for certain game objects, execution loops.”

For example, when creating a character in a game, the programming of this character may have a number of functions that should be checked, and executed in every single frame of the game.

Different game engines have different names, and ways to implement these procedures. Here we’ll see how it works on Unity3D.

Create a scene in Unity, and add two cubes to it, and name them as “Cubo1” and “Cubo2″.

[code lang=”javascript”]
private var teste:int = 0;

function Update () {
Debug.Log(“Frame : “+teste);
teste++;
}
[/code]
Obs.: Create this script, name it as you wish. Drag the script, from the “Project” panel, to one of the game objects in Hierarchy panel (prefer one of the cubes). As soon as you run the game, you will see the output in the Console window.

This script, counts the number of the frames in your game. This code, increments in 1, the value in teste variable. In the first frame it is zero, than it is incremented to one; and so on.

Now, lets make the cube to move:

[code lang=”javascript”]
private var teste:int = 0;

function Update () {

transform.Translate(Vector3.right * Time.deltaTime);
Debug.Log(“Frame: “+teste);
teste++;
}
[/code]

Now, the script makes the cube moves across the X axis.
Take a look at the sample here.