---
title: "Moving the Player"
content_type: tutorial
url: "https://learn.unity.com/tutorial/moving-the-player?language=en"
language: en
difficulty: beginner
duration_minutes: 30
unity_version:
  - "6.0"
  - "6.3"
supported_unity_versions:
  - name: "2022.3"
    url: "https://learn.unity.com/tutorial/moving-the-player.md?language=en&version=2022.3"
  - name: "2020.2"
    url: "https://learn.unity.com/tutorial/moving-the-player.md?language=en&version=2020.2"
  - name: "2020.1"
    url: "https://learn.unity.com/tutorial/moving-the-player.md?language=en&version=2020.1"
  - name: "2019.4"
    url: "https://learn.unity.com/tutorial/moving-the-player.md?language=en&version=2019.4"
  - name: "2019.3"
    url: "https://learn.unity.com/tutorial/moving-the-player.md?language=en&version=2019.3"
  - name: "6.0"
    url: "https://learn.unity.com/tutorial/moving-the-player.md?language=en&version=6.0"
  - name: "6.3"
    url: "https://learn.unity.com/tutorial/moving-the-player.md?language=en&version=6.3"
last_updated: "2026-08-04T14:21:58.000Z"
---

# Moving the Player

Add a Rigidbody component, script a PlayerController in C#, and test and debug movement for the Roll-a-ball player sphere.

 In the second Roll-a-ball tutorial, you’ll:

- Add a Rigidbody to the Player sphere, so you can use the Unity physics engine
- Write your own PlayerController script in C#, to make the sphere respond to player input
- Test your script and fix errors

## Overview

In this tutorial, you’ll:

- Add a Rigidbody to the Player sphere, so you can use the Unity physics engine
- Write your own PlayerController script in C#, to make the sphere respond to player input
- Test your script and fix errors


By the end of this tutorial, your game will look something like this:

[Video](https://connect-mediagw.unity.com/h1/20230920/videos/7194fb27-0b8b-4a07-a3ea-faee90a6568c_2.8.4-tutorial-final-ball-moving.mp4)

To use this tutorial, you will need a Unity project with a Scene containing the ball and the surface it rolls on. If you haven’t already created these, start with the tutorial [Setting up the Game](https://learn.unity.com/tutorial/66f2da05edbc2a01279b91c7).

## Add a Rigidbody to the Player 

Follow the video or text instructions below to add a **Rigidbody** component to the **Player** GameObject:

[Video](https://connect-mediagw.unity.com/h1/20211215/videos/3c31ac30-e7e9-4123-9a11-b743a9297c6c_Unity_RollABall_2_1.mp4)

[Download Transcript](https://connect-prd-cdn.unity.com/20200716/2f631135-5f40-49c5-97c7-f3fec3a696ee/Add_a_Rigidbody_to_the_Player-Transcript.txt?_ga=2.11565628.943030393.1594530423-42703541.1592249374)

## Add a Rigidbody component.

- Select the **Player** GameObject in the **Hierarchy** window.
- In the **Inspector** Window, select **Add Component**, then search for "Rigidbody" and add the **Rigidbody** component to the **Player** GameObject.

**Note**: Make sure to select **Rigidbody** and not **Rigidbody 2D**.

![](https://connect-mediagw.unity.com/h1/20230920/learn/images/2284a70a-4c83-4a8b-b4e6-81715c7197cc_1-rigidbody-comp-selected.png)

## Add a Player Input component

Follow the video or text instructions below to add the **Player Input** component:

[Video](https://connect-mediagw.unity.com/h1/20240927/videos/788b46cf-f00e-4217-96a6-c7881c1e4ea2_Unity_RollABall_2.3_U6.mp4)

[Download Transcript ](https://unity-connect-prd.storage.googleapis.com/20240924/77994199-9628-4a98-b83b-ca33152f0fda/Add-a-Player-Input-Component-Transcript.txt)

## 1\.  Add the Player Input component.

- Select the **Player** GameObject in the **Hierarchy** window.
- In the **Inspector** window, add the **Player Input** component.

## Create a new script

Follow the video or text instructions below to create a new **PlayerController** script and open it in a script editor:

[Video](https://connect-mediagw.unity.com/h1/20211215/videos/33588d92-b46f-4fc0-8342-a67ef28c66ff_Unity_RollABall_2_4.mp4)

[ Download Transcript](https://connect-prd-cdn.unity.com/20200716/321f4ca0-f944-4680-9b69-145037a69b99/Create_a_new_script-Transcript.txt?_ga=2.14781758.943030393.1594530423-42703541.1592249374)

## 1\.  Create a new PlayerController script.

- In the **Project** window, create a new folder under Assets, named "Scripts".
- With the **Player** GameObject selected, select **Add Component** > **New Script,** then name the new script "PlayerController".
- The created script asset will be at the root level of the **Assets** folder by default. Move the new **PlayerController** script asset into the **Scripts** folder.


## 2\.  Open the script in a script editor.

- Double-click the script asset in the **Project** window to open it in your preferred script editor, usually Visual Studio.

![](https://connect-mediagw.unity.com/h1/20230920/learn/images/4c96c305-1042-40ab-a0c1-3ef38187f0a2_4-create-new-playercontroller.png)

## Write the OnMove function declaration

Follow the video or text instructions below to delete the Update function, add the InputSystem namespace, and add the OnMove function:

[Video](https://connect-mediagw.unity.com/h1/20240927/videos/41fa6405-1e3c-4922-9a30-28cbe0854830_Unity_RollABall_2-5_U6_V2.mp4)

[Download Transcript](https://unity-connect-prd.storage.googleapis.com/20240924/187aca5c-4e4a-48df-ab5c-9618c3505f8e/Write_the_OnMove_function_Declaration-Transcript.txt)[ ](https://connect-prd-cdn.unity.com/20200716/850d9bae-6d3d-4abd-968c-4fa76ada8c18/Write_the_OnMove_function_Declaration-Transcript.txt?_ga=2.44547756.943030393.1594530423-42703541.1592249374)

## 1\.  Delete the Update function.

- If you don’t already have the **PlayerController** script open, open it now.
- Delete the entire **Update** function — you won’t need it.


## 2\.  Add the InputSystem namespace.

- Add the following line of code to the top of the script under the existing namespaces:

```csharp
using UnityEngine.InputSystem;
```

## 3\.  Add the OnMove function.

- Below the **Start** function but before the final curly brace, add a new line and add the following code:

```csharp
   void OnMove (InputValue movementValue)
   {
   
   }
```

## Apply input data to the Player

Follow the video or text instructions below to set the movement vector, assign a new Rigidbody variable, and add a FixedUpdate function:

[Video](https://connect-mediagw.unity.com/h1/20211215/videos/5449e66d-a202-49ef-bbcc-227c6d476d0a_Unity_RollABall_2_6.mp4)

[Download Transcript](https://connect-prd-cdn.unity.com/20200716/8a714aad-8120-43a5-90cc-32067d15d8c4/Add_input_data_to_the_Player-Transcript.txt?_ga=2.116462862.943030393.1594530423-42703541.1592249374)

## 1\.  Set the movement vector.

- In the space inside the **OnMove** function, add the following line of code:

```csharp
Vector2 movementVector = movementValue.Get<Vector2>(); 
```

## 2\.  Assign a new Rigidbody variable.

- Above the **Start** function, add the following line of code to declare a new variable:

```csharp
private Rigidbody rb; 
```

- Next, inside the **Start** function, add the following line of code to assign the variable’s value:

```csharp
rb = GetComponent <Rigidbody>(); 
```

## 3\.  Add a FixedUpdate function.

- Create a new function called **FixedUpdate** below the **Start** function by adding the following code:

```csharp
   void FixedUpdate() 
   {
   
   }
```

## Apply force to the Player

Follow the video or text instructions below to add force to the player, add x and y movement variables, assign those variables, finalize the **AddForce** call, and test your game.

[Video](https://connect-mediagw.unity.com/h1/20211215/videos/20080a58-38f8-4b3c-a8d2-54b1cfc1f817_Unity_RollABall_2_7.mp4)

[Download Transcript](https://connect-prd-cdn.unity.com/20200716/4038fb45-ddab-452b-a41e-c8c18f1ef874/Apply_force_to_the_Player-Transcript.txt?_ga=2.77159325.943030393.1594530423-42703541.1592249374)

## 1\.  Add force to the player.

- In the **FixedUpdate** function body, add the following code:

```csharp
rb.AddForce(movementVector); 
```

- If the Editor shows an error, you can ignore it for now.


## 2\.  Add x and y movement variables.

- Underneath the **Rigidbody** variable you created earlier, add two more private variables called movementX and movementY of type float:

```csharp
private float movementX;
private float movementY;
```

## 3\.  Assign the movement variables.

- In the **OnMove** function, add the following two lines of code:

```csharp
movementX = movementVector.x; 
movementY = movementVector.y; 
```

## 4\.  Finalize the AddForce call.

- In the **FixedUpdate** function, add the following new line at the top of the function:

```csharp
Vector3 movement = new Vector3 (movementX, 0.0f, movementY);
```

- Revise the second line of code in **FixedUpdate** to include the movement variable:

```csharp
rb.AddForce(movement);
```

## 5\.  Test the game.

- The **Player** GameObject should move, but slowly — you’ll fix that in the next step.

[Video](https://connect-mediagw.unity.com/h1/20230920/videos/f63e739a-71a8-466f-930a-fc11f968e868_2.7.5-test-movement-slow.mp4)

## Fix the Player movement speed

Follow the video or text instructions below to declare a new speed variable, multiply the force by speed, increase the speed in the **Inspector** window, and test the game:

[Video](https://connect-mediagw.unity.com/h1/20211215/videos/7ba0111e-a5bf-44d1-88dd-fbf0727c0731_Unity_RollABall_2_8.mp4)

[Download Transcript ](https://connect-prd-cdn.unity.com/20200716/50c58232-b21a-449c-9512-71b122ae8590/Fix_the_Player_movement_speed-Transcript.txt?_ga=2.119970688.943030393.1594530423-42703541.1592249374)

## 1\.  Declare a new speed variable.

- Create a new public float variable at the top of the script:

```csharp
public float speed = 0; 
```

## 2\.  Multiply the force by speed.

- In the **FixedUpdate** function, revise the **AddForce** call to include the speed variable:

```csharp
rb.AddForce(movement * speed); 
```

## 3\.  Increase the speed in the Inspector window.

- Since the speed is **0**, the player won’t move.
- In the **PlayerController** component, set the speed value to **10** and test your game.

![](https://connect-mediagw.unity.com/h1/20230920/learn/images/751a9a84-ba80-4aaf-909b-3047dfd92f3f_8-speed-variable-in-inspector.png)

## 4\.  Test the game.

- The **Player** GameObject should now move at a more reasonable speed.
- You can adjust the speed up or down if you want.

[Video](https://connect-mediagw.unity.com/h1/20230920/videos/c8217ba4-4aec-4005-ae27-ee984b7104d2_2.8.4-tutorial-final-ball-moving.mp4)

## Final script sample _(Optional)_

If your script is not working as expected, below is an example of what your code should look like at this point. The comments have been added to make the code more readable.


## PlayerController.cs

```csharp
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
 // Rigidbody of the player.
 private Rigidbody rb; 

 // Movement along X and Y axes.
 private float movementX;
 private float movementY;

 // Speed at which the player moves.
 public float speed = 0; 

 // Start is called before the first frame update.
 void Start()
    {
 // Get and store the Rigidbody component attached to the player.
        rb = GetComponent<Rigidbody>();
    }
 
 // This function is called when a move input is detected.
 void OnMove(InputValue movementValue)
    {
 // Convert the input value into a Vector2 for movement.
        Vector2 movementVector = movementValue.Get<Vector2>();

 // Store the X and Y components of the movement.
        movementX = movementVector.x; 
        movementY = movementVector.y; 
    }

 // FixedUpdate is called once per fixed frame-rate frame.
 void FixedUpdate() 
    {
 // Create a 3D movement vector using the X and Y inputs.
        Vector3 movement = new Vector3 (movementX, 0.0f, movementY);

 // Apply force to the Rigidbody to move the player.
        rb.AddForce(movement * speed); 
    }
}
```
