How to Create Simple Player Movement in Unity

Simon Pham
5 min readJun 21, 2023

--

Objective: The goal of this blog post is to provide a step-by-step guide on how to create a simple movement for the player in Unity.

Assuming I have a Player object in Unity and want to enable the player to control it with their input, I would first need to create a C# script and attach it to the Player object.

We can start by creating a folder to store all of our script files. To do this, right-click on the Project panel, select Create > Folder, and name it “Scripts”.

To create a script file, right-click on the Scripts folder, select Create > C# Script, and name it “Player”. Clicking on the Player script will display it in the Inspector. Note: The script name must match the class name (in this case, Player), otherwise Unity will throw an error.

public class Player : MonoBehaviour

Next, drag the script file into the Inspector with the Player object selected to link the script with the object.

Here are the steps we will complete to create simple movements for our Player object:

Step 1: give the Player object a start position of (0,0,0) when the game starts
Step 2: collect and store user movement input in a variable
Step 3: convert user input to movement

Step 1: Start Position

If you click on the Player object in the Hierarchy panel, you will notice that it has a Transform component that controls the Position property.

To set up a start position, we will use dot notation to access the position property and assign the position (0, 0, 0) to it using Vector3.

transform.position = new Vector3(0,0,0);

This is how the Player script appears when we open it for the first time. Right from the start, Unity provides us with two default functions: void Start(), which is called before the first frame update, and void Update(), which is called once per frame.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{

}
}

So, we need to place the Vector3 function inside the void Start() function. This will result in the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
tranform.position = new Vector3(0,0,0)
}
// Update is called once per frame
void Update()
{

}
}

Step 2: Collect and Store User Input in a Variable

To allow user control, we need to collect their input. We can do this with Input Manager. Go to the Edit menu, select Project Settings, and click on Input Manager.

In the Input Manager, expand the Axes list. By default, Unity provides a list of 18 different Axes for developers to map the user input but we’re only interested in the Horizontal and Vertical Axes. The Horizontal Axis maps to left and right (or A and D) keys, while the Vertical Axis maps to up and down (or W and S) keys.

We need two private float variables to store the horizontal and vertical input. To follow common practice, prefix them with an underscore (_)

private float _horizontalInput;
private float _verticalInput;

We will assign values to these variables using the GetAxis() method on the Input class, and assign the value to a variable called direction (with a value of 0 for the Z axis).

_horizontalInput= Input.GetAxis("Horizontal");
_verticalInput = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(_horizontalInput, _verticalInput, 0);

Step 3: Convert User Input to Movement

To convert user input into movement, we need a variable for the speed of the object. Let’s assign a value of 3.5f to it (you can choose a value that you prefer). We can use the SerializeField attribute to make private variables visible in the Inspector.

[SerializeField]
private float _speed = 3.5f;

Next, we will use the Translate() method on the Transform component and pass the direction variable as a parameter.

transform.Translate(direction * _speed * Time.deltaTime);

The _horizontalInput and _verticalInput variables can vary between -1.0 and 1.0, depending on the user input. Since the method void Update() is called once per frame, multiplying the input variables by Time.deltaTime converts the values to meters per second. Additionally, by multiplying the result by the _speed variable, we can move the object at a speed of 3.5 meters per second.

Here’s the final code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
[SerializeField]
private float _speed = 3.5f;
private float _horizontalInput;
private float _verticalInput;

// Start is called before the first frame update
void Start()
{
transform.position = new Vector3(0, 0, 0);
}

// Update is called once per frame
void Update()
{
_horizontalInput = Input.GetAxis("Horizontal");
_verticalInput = Input.GetAxis("Vertical");

Vector3 direction = new Vector3(_horizontalInput, _verticalInput, 0);

transform.Translate(direction * _speed * Time.deltaTime);
}
}

This is the final result of the player movement implementation.

We can improve the cleanliness of our code by creating a dedicated function for movement called CalculateMovement(). We can then move all the code related to movement into this function. Additionally, we should use this function within the void Update() function. Here’s the final code:

 void Update()
{
CalculateMovement();
}

void CalculateMovement()
{
_horizontalInput = Input.GetAxis("Horizontal");
_verticalInput = Input.GetAxis("Vertical");

Vector3 direction = new Vector3(_horizontalInput, _verticalInput, 0);

transform.Translate(direction * _speed * Time.deltaTime);

//restrict players vertical movement in the screen
transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -5, 5), 0);

//wrap players horizontal movement in the screen
if (transform.position.x > 9.40f)
{
transform.position = new Vector3(-9.40f, transform.position.y, 0);
} else if (transform.position.x < -9.40f)
{
transform.position = new Vector3(9.40f, transform.position.y, 0);
}
}

Note: I’ve added some code to restrict the player’s movement, preventing them from going beyond the vertical boundaries of the screen. Additionally, if the player exceeds the horizontal boundaries of the screen, they will reappear on the opposite side of the screen.

--

--

No responses yet