Player movement in Unity with Rigidbodies

Player movement in Unity with Rigidbodies and Colliders

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.

Player movement in Unity with Rigidbodies
Player movement in Unity with Rigidbodies – Create the Ground

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.

Player movement in Unity with Rigidbodies
Player movement in Unity with Rigidbodies – Scale 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.

Player movement in Unity with Rigidbodies
Player movement in Unity with Rigidbodies – Create 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

Player movement in Unity with Rigidbodies
Player movement in Unity with Rigidbodies – Configuring the Inputs in your game


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.

Player movement in Unity with Rigidbodies
Player movement in Unity with Rigidbodies – Configuring the Controls

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.

HOW TO MAKE A COMBO FIGHTING SYSTEM IN UNITY

HOW TO MAKE A COMBO FIGHTING SYSTEM IN UNITY

In this article we are compiling some tutorials HOW TO MAKE A COMBO FIGHTING SYSTEM IN UNITY. When implementing combat systems you can different ways to implement the attacks systems, one of the most used is the combo system, and how you can connect several successive attacks to perform special and more powerful attacks.

What is a combo system?

In video games, a combo (short for “combo”) is a series of actions, usually under strict timing restrictions, that result in a significant gain or advantage. The term originated in fighting games, which are based on the concept of combinations of blows. It has since been applied to a variety of genres, including puzzle games, shooting games, and sports games. Combos are generally used as an integral part of gameplay, but they can also be used to adjust high scores and attack power, or simply as a means to express a flashy play style.

In fighting games, a combo refers specifically to a series of well-timed moves that produce a cohesive sequence of strikes, each of which makes the opponent unable or almost unable to block or evade the next.

HOW TO MAKE A COMBO FIGHTING SYSTEM IN UNITY

HOW TO MAKE A COMBO FIGHTING SYSTEM IN UNITY

This tutorial is part of a 2D platformer series in Unity that will guide you through the process of implementing a 2D game with a combo fighting mechanic.

HOW TO MAKE A COMBO FIGHTING SYSTEM IN UNITY


Easy 2D Melee Combo System in Unity by ChosenPlay

This tutorials teaches you an Easy solution at coding a 2D Melee Combo System inside the Unity Game Engine.

Correctly coding it can be frustrating: the majority of solutions you’ll think of or find on the internet will be either overly complicated or not even work.

This tutorial you’ll be able to code your own 2d melee combo system in unity with:

– No Timers

– No Coroutines

– And Just a few lines of code

HOW TO MAKE A COMBO FIGHTING SYSTEM IN UNITY

Combo attack with one button in Mobile (Unity)/ by using animation events by Khamy

As mentioned in the above tutorials, Combo attacks are an essential part of an action game, in this tutorial you will see how to implement them using Animation Events that can be used too to make combo attacks in a very easy.

HOW TO MAKE A COMBO FIGHTING SYSTEM IN UNITY

You can find more tutorials on Unity in our blog