Learn a simple way to program a Player movement in Unity with Rigidbodies and Colliders components.
Are you new to Unity? Want to learn how to take player information and move the character around the screen? This guide will show you three different ways to control player movement in Unity.
Whether you are a beginner or an experienced programmer, this C# tutorial will help you get your feet and your character moving.
So let’s get started.
Create a SandBox
In order to test what we will be implementing in the tutorial, let’s create a small sandbox.
First create a Cube GameObject, form the Object Menu 3D object and cube.
Scale it on the X and Z axis to create a ground.
Rename the game object to Ground.
Let’s add a new game object, a Cube or a Sphere.
In the inspector, click on the ADD Component and choose Rigid Body from the list.
Rename the object to Player.
Save the scene as SandBox.
Moving the player in Unity: The Input Manager
First of all, you need to know how to take user input and turn it into in-game movement – in Unity, this is quite simple, provided you know where to look.
Unity’s project settings
With Unity open, click ‘Edit’ from the top toolbar. Next, select ‘Project Settings’. From the list on the left, select ‘Input Manager’, select Axes and enter a list of input values.
For basic movements, you will see ‘Horizontal’ and ‘Vertical’, configures the controls to the ones that best suits your project, or if not, leave the default ones.
In the next section, we will use these axes and Input.GetAxisRaw(); to do some basic motion.
Using Rigidbody and Collider to move the player in Unity
Now that we know the names of the axes, we can use them to control the player’s movement.
In the Hierarchy view of the Unity project, right-click and select 3D Object > Capsule to create the one that will be given the movement. Similarly, create an Earth Planet for the Capsule to stand on.
Redefine the transformation values for both objects and move the Capsule so that it stands on the plane. Rename the capsule to “Player” or something similar for clarity.
Click on the Player object and in the inspector view, scroll down to Add Component, add a Rigidbody and then another component, this time as a Capsule Collider. These components are needed to add physics, or motion, to the Player.
Next, right click on the Scripts folder and create a new C# script. Give this script the name “PlayerController.” If you want to add more than one movement type for different characters or controllers, you will need to create a number of scripts for each movement type. Here, we will focus on the basics and use one script.
Double click on the script to open it. You will see the default Unity script.
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
The code above is the standard one, and won´t do anything 🙂
Let’s we create a public float variable called velocity or something similar. This velocity variable is a multiplier, and with a little more programming, we will be able control how fast the Player will be moving. Right now, velocity is set to a value such as 10f or 10 meters per second.
We also need to let Unity know that we have a Rigidbody to manipulate with this script. This is done using the Rigidbody keyword and a variable name, in this case rb.
public class PlayerController : MonoBehaviour
{
public float speed = 10f; //Controls the player velocity
Rigidbody rb; // Uses the variable rb to reference it the RigidBody Component in the GameObject that we have created earlier on
This is all that is added in this section. Now, let’s move to the Start() function that is executed when the game starts running and where we should try save in rb the reference to the rigid body component.
void Start()
{
rb = GetComponent<Rigidbody>(); //rb gets the rigidbody on the player
}
Now let’s look at the Update() function. This is the function that runs once per frame or in a regular system each 4 milliseconds, and we will try to get an input from the player’s keyboard, controller, etc.
Remember how we checked the input axis in the project definitions? Use it here.
void Update()
{
float dirX = Input.GetAxis("Horizontal"); // changes value between 1 and -1
float dirZ = Input.GetAxis("Vertical"); // changes value between 1 and -1
rb.velocity = new Vector3(dirX, rb.velocity.y, dirZ) * speed; // Creates velocity in direction of value equal to the controls or the directional keys. rb.velocity.y deals with falling because it will hold the gravity, and later on will allow us to jump.
}
First, create a float variable with a name like dirX, and set it equal to Input.GetAxis(“Horizontal”); and which will hold the value of any changes in the control inputs used for moving left and right. The values will float between -1 to 1.
Input.GetAxis(); is Unity’s method for recording player input from Axes found in the project definitions. For more information, see the official Unity documentation.” Horizontal” comes from the name Horizontal Axis in Unity. This axis controls left/right movement with the key settings defined in the project settings.
As you may already know, the float dirZ = Input.GetAxis(“Vertical”); is the same as the vertical control, but we will use it to move the Z axis.
Next, you’ll put that speed variable you created into play and complete the player movement in Unity.
rb.velocity = new Vector3(dirX, rb.velocity.y, dirZ) * speed; // Creates velocity in direction of value equal to the controls or the directional keys. rb.velocity.y deals with falling because it will hold the gravity, and later on will allow us to jump.
Go back to the Inspector view in Unity and show the Player object, look at the Rigid Body, under Info, there is a value called Velocity. This is the value we are targeting with rb.velocity.
The new Vector3(dirX, rb.velocity.y, dirZ) * speed creates a new vector with the given x, y, z values and multiplies that vector value by speed.
Drag the PlayerController script to the Player object in Unity and you’re done. You now have a C# script that receives the player input and converts it into character movement in Unity.
This is the completed code.
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 10f; //Controls the player velocity
Rigidbody rb; // Uses the variable rb to reference it the RigidBody Component in the GameObject that we have created earlier on
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>(); //rb gets the rigidbody on the player
}
// Update is called once per frame
void Update()
{
float dirX = Input.GetAxis("Horizontal"); // changes value between 1 and -1
float dirZ = Input.GetAxis("Vertical"); // changes value between 1 and -1
rb.velocity = new Vector3(dirX, rb.velocity.y, dirZ) * speed; // Creates velocity in direction of value equal to the controls or the directional keys. rb.velocity.y deals with falling because it will hold the gravity, and later on will allow us to jump.
}
}
Any doubt or any problem? Leave them in the comment section.
Want more great contents and tutorials, check our main blog or our youtube channel.