Creating a Simple Movement Script in Unity Using the RigidBody Component

I am a Game Developer and VR Developer with a year of experience working with Unity. I am passionate about creating fun games and interactive experiences, and I love to share what I learn, as well as my solutions to problems I face.
Building a new game comes with a lot of planning, design, and scripting. Depending on the type of game you are working on, you would most likely need to create a player movement script. This is usually the first script you create when starting your new project.
In this article, I will explain a simple method to create a player movement script in Unity for your games.
What is a RigidBody Component?
A RigidBody component is a component that allows a GameObject to react to real-time physics in Unity. This includes reaction to forces, gravity, mass, drag, and momentum.
Setting Up Your Project
To prepare your project, you would need to:
- Create a GameObject by right-clicking in the Hierarchy panel, select GameObject, and name it "Player".
- Create a Scripts folder in your project window by right-clicking and selecting folder.
- In your scripts folder, right-click and create a C# script named "Player".
- Assign your script to the game object by simply dragging and dropping it on the game object in your hierarchy.
Creating a Movement Script With RigidBody Component
To use this method, you would need to add a RigidBody component to your game object by going in the inspector, clicking add component and searching for Rigidbody. Now go back into your script and follow the steps below.
Step 1: Creating Your Variables
Your default script should open with Start and Update methods already in it. It should look like this:
using UnityEngine;
public class Player : MonoBehaviour
{
void Start()
{
}
void Update()
{
}
}
Above the Start method, we will declare some variables. These variables are:
- A RigidBody named
playerRbto store your RigidBody Component. - A float named
speedto increase or decrease movement speeds. Letspeed = 10. - Floats named
verticalInputandhorizontalInput
Your script should look like this:
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody playerRb;
public float speed = 5;
private float horizontalInput;
private float verticalInput;
private void Start()
.
.
The speed variable is made public so it can be changed in the inspector.
Step 2: Initializing Your RigidBody Component
This is done in the Start method using GetComponent. GetComponent allows us to access scripts or components in a GameObject.
We use GetComponent so that an instance of the RigidBody component of our GameObject is stored when the game starts.
void Start()
{
playerRb = GetComponent<Rigidbody>();
}
Step 3: Getting Your Inputs With Input.GetAxis()
Input.GetAxis() allows you to receive your vertical and horizontal input based on their respective axes. The Input.GetAxis() will always return a value within the range of -1 to +1. This is important to define left, right, up and down movements.
So -1 on the horizontal axis would mean left and +1 would mean right, while -1 on the vertical axis would mean down and +1 would mean up. The keywords representing the individual axis in the parenthesis can be viewed or edited in the Input Manager of your project settings.

This is done in the Update method since it refreshes every frame, and we also want to check for an input every frame, during game runtime.
Your script should look like this:
void Update()
{
//Horizontal Input
horizontalInput = Input.GetAxis("Horizontal");
//Vertical Input... forward/backward
verticalInput = Input.GetAxis("Vertical");
.
.
Step 4: Applying Velocity to Your RigidBody
As explained earlier, adding a RigidBody component allows you to apply physics to your GameObjects. We access velocity by using playerRb.velocity.
A new Vector3 is assigned to playerRb.velocity so we can update the x, y and z coordinates. i.e Vector3(x , y ,z). Where x is the horizontal, y is the vertical and z represents forward/backward.
In this case, x is the horizontalInput, y doesn't need to be changed because we are not moving up and down, and z is the verticalInput.
playerRb.velocity = new Vector3(horizontalInput , playerRb.velocity.y, verticalInput);
Running this in Unity will result in a very slow player. We do not want that. So, we multiply the horizontal and vertical input by the speed variable we created earlier. Multiplying will increase or decrease the speed. You can edit this in the player inspector as it is public.
Finally, save your script and run your project.
Your final script should look like this:
using UnityEngine;
public class Player : MonoBehaviour
{
Rigidbody playerRb;
[SerializeField] float speed = 10;
float horizontalInput;
float verticalInput;
void Start()
{
playerRb = GetComponent<Rigidbody>();
}
void Update()
{
//Horizontal Input... A or left arrow for left(-1), D or right arrow for right(+1)
horizontalInput = Input.GetAxis("Horizontal");
//Vertical Input... W or up arrow for up(+1), S or down arrow for down(-1)
forwardInput = Input.GetAxis("Vertical");
//vertical and horizontal movement
playerRb.velocity = new Vector3(horizontalInput * speed, playerRb.velocity.y, verticalInput * speed);
}
}
Did It Work?
I hope this tutorial was able to help you create a functioning player movement script.
If you have any suggestions, or another approach to this, don’t hesitate to share them in the comments.