Videogames Development – Unity – How to add WASD

PREFERENCES

Horizontal

MAIN TOP MENU> Edit> Project Settings> Input> Inspector> Horizontal:
Negative Button: left -> Alt Negative Button: a
Positive Button: right -> Alt Positive Button: d

Vertical

MAIN TOP MENU> Edit> Project Settings> Input> Inspector> Vertical:
Negative Button: down -> Alt Negative Button: s
Positive Button: up -> Alt Positive Button: w

NOTICE: they are classic – W A S D – controller for left hand

SCRIPT

MAIN TOP MENu> ASSETS> CREATE> Javascript, type the name ‘GetInput’

Assets> GetInput> Inspector> ‘Open…’ button

Write:

#pragma strict

function Start () {

}

function Update () {

 var horiz : float = Input.GetAxis("Horizontal");
 Debug.Log(horiz);

}

Create an Object inside the Hierarchy ‘MyObject’

Assets> DRAG AND DROP ‘GetInput’ script over Hierarchy> MyObject

Play> Use – A D – buttons, look the BOTTOM OF THE SCREEN, click label ‘Console’ to see the input values.

Write:

#pragma strict

function Start () {

}

function Update () {

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

Play> Use – A D – buttons to move object

NOTICE:

transform.Translate(Vector3(horiz,0,0));

It gets X position from horizon variable and Translate (move) the Object