Unity – Input.GetAxis – Movement Behaviour

MOVEMENT BEHAVIOUR

NOTICE:
-> Input.GetKey e Input.GetButton -> return a BOOLEAN VALUE true or false
-> Input.GetAxis of keyboard and joystik -> return a VALUE from 1 to -1

The value will be in the range -1…1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1…1.

1. Create virtual Axes and buttons can be created in: MAIN TOP MENU> Edit> Project Settings> Input.

2. Setup Virtual Axis from right column.

unity-geyaxis

How does it works:
1. A position, the joy is not being pressed -> value returned 0.00
2. When we first press the key, in the first frame -> value returned is 0.01
3. We progress througth frames holding down the button (mantengo premuto) -> value returned increase
4. Holding down the button at the end -> value returned is 1
5. We release the button, in the first frame -> value returned is 0.99
6. We progress througth frames (button is released) -> value returned decrease
7. At the end -> value returned 0.00

Parameters of MAIN TOP MENU> Edit> Project Settings> Input> Horizon:

– Setup Axis
Positive Button: right ()

– Assign buttons:
Alt Positive Button: d
End users can configure Keyboard input in a nice screen configuration dialog

– Gravity, how fast the scale return to 0 after the button has been released (B -> A)
> gravity > fast return
< gravity < fast return 0.1: slow 3 : default 100: fast - Sensitivity, how fast the scale reaches 1 (A -> B) – unity x seconds
> sensitivity > responsive > fast
< sensitivity < resposnsive < fast 0.1: slow 3 : default 100: fast - Dead (to prevent unwanted little joystick movement) > dead zone < joystick sensibility < dead zone > joystick sensibility

– Snap
If enabled, the axis value will be immediately reset to zero after it receives opposite inputs. Only used when the Type is key / mouse button.

3. DRAG AND DROP the script over an object in the Hierarchy
Example: move an object using GetAxis – Horizontal

#pragma strict

function Start () {

}

function Update () {

 var horiz : float = Input.GetAxis("Horizontal");
 Debug.Log(horiz);
 transform.Translate(Vector3(horiz,0,0));
}

Statement: static function GetAxis(axisName: string): float;
axisName -> Virtual Axes names

Horizontal and Vertical Control – Complete Example

#pragma strict

function Start () {

}

var speed : float = 10.0; // to change speed

function Update () {

 var horiz : float = Input.GetAxis("Horizontal");
 var vert : float = Input.GetAxis("Vertical");

 // Make it move 10 meters per second instead of 10 meters per frame...
 horiz *= Time.deltaTime;
 vert *= Time.deltaTime;
 
 transform.Translate(Vector3(horiz*speed,vert*speed,0));
}