Simple Player Movement in Unity

In this article, we will create a GameObject that moves up, down, left, and right based on the user’s input. This should help us to understand the workflow of Unity scripting more easily.

In order to implement player movement, we need a GameObject in the scene to act as our player. Add a cube in the Hierarchy window and name it “Player”.

Every GameObject has at least one component — Transform. The Transform of a GameObject also shows up as variables in the scripting side of Unity so we can modify it via code. This is not restricted to the Transform either; all components in Unity have properties, which are accessible through variables in scripting.

Let us start with our movement script. Create a new script, and name it “Player_Movement”.

Now add this script to the player by dragging the player script onto the Player in the hierarchy. If you view the Player in the Inspector window you should see the Player script has been added as a component.

Open the script and create a private float variable named speed, which can only be accessible from the player script but let's make sure that our game designers can change the speed in the inspector.

If we save this script without touching the other methods, it should compile in Unity and we should be able to see the Speed variable in the Inspector window.

Since the speed value is adjustable and needs not to be changed in code all the time, we can use the update() method instead of start().

Let us now consider the objectives for the Update method:

  1. Check for the user input.
  2. If there is user input, read the directions of input.
  3. Change the position values of the object’s transform based on its speed and direction.

Let's use Unity’s Input System to control the player movement.

We will use the Horizontal for movement on the X-axis and Vertical for movement on the Y-axis.

public class Player_Movement : MonoBehaviour
{
[SerializeField] private float speed = 10.0f;
private void Start()
{

}
private void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");

Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);
transform.Translate((direction * (speed * Time.deltaTime)));
}
}

We make a floating-point variable named horizontalInput (for horizontal), and its value is given by the Input.GetAxis method. This method returns -1, 0, or 1 depending on which key the player has pressed on the up/down/left/right arrows.

The Input class is responsible for getting input from the user in the form of key presses, mouse input, controller input, and so on.

Later, we are updating the position of our player to a new position defined by creating a new Vector3. The Vector3 takes 3 parameters, which are its x, y, and z values respectively.

Finally translating the transform of the player by multiplying the direction (from User Input) with the speed variable and time.

Save this script and head back to Unity.

Now click “Play” and try pressing the arrow buttons to see the player moving around based on our input. To stop the game, simply press Play again. You can even adjust the speed in real-time so you do not have to stop and start it all the time.

A VR/AR developer, who enjoys making games and developing interactive environments using Unity’s XR integration toolkit for Oculus quest and HTC vive devices.