Simple Player Movement in Unity
Variables that hold user input values are contained in the Update() block of Unity code.
There is a hierarchical list structure to identify the input methods Unity uses to pass information to the Update() block during a project (game) run.
From the Unity interface, click the drop down menu <Edit> and pick <Project Settings> from the menu list.
If this is the first time, the project may need to be linked to your Unity Cloud organization. Here is the example using the organization “xcvvc” (botom row of keyboard, easy to type).
With that link established, examine the very full tree list of Project Settings and <click> on “Input Manager”. Moving the gameObject’s along it’s Horizontal and Vertical axes.
In particular this relates to the keyboard <left-arrow>, <right-arrow>, ‘a’ and ‘d’ keys for Horizontal movement.
Returning to Visual Studio, the hierarchy can be coded into the user input inside an Update method.
Here are three examples of x-axis and y-axis input:
Double transform:
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.right * horizontalInput * _speed * Time.deltaTime);
transform.Translate(Vector3.up * verticalInput * _speed * Time.deltaTime);
Long transform:
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(new Vector3(horizontalInput, verticalInput,0) * _speed * Time.deltaTime);Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);
Short transform:
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);
transform.Translate(direction * _speed * Time.deltaTime);