Simple Player Movement in Unity

Lance Gold
2 min readMar 8, 2024

--

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).

Important linking to Unity Cloud to access Project Services

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.

The Project Settings Input Manager example listing Horizontal services.

Returning to Visual Studio, the hierarchy can be coded into the user input inside an Update method.

use Visual Studio’s keyword suggestions to navigate through the object tree

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);

--

--

No responses yet