My journey becoming a Unity game developer: 2.5D Infinite Runner-Player movement with velocity and gravity applied

Rhett Haynes
3 min readDec 16, 2021

--

Objective: Make the Player game object move along the X-axis applying velocity. Also, add gravity to the player to keep them grounded as they move.

Player moving with velocity and gravity.

To create movement for the player, we will start by adding the Character Controller component to the Player object. The Character Controller doesn’t use Rigidbody physics, but automatically comes with a Capsule Collider attached to it. To learn more about the Character Controller, visit the following Unity page: https://docs.unity3d.com/Manual/class-CharacterController.html

Character controller component added to the Player game object.

Next, from the Scripts folder we will create a new C# script name Player and attach it to the Player game object. Create 2 new variables where the 1st one will have a Type CharacterController name controller to be a handle to the Character Controller component attached to the Player game object. The 2nd variable will be a Type Vector 3 name direction to represent the direction the Player will be allowed to move in.

Basic back and forth movement for the player.

Play the game using the left and right arrow keys to move the Player along the X-axis one meter per second.

Player moving one meter per second.

To make the player move in a direction faster than one meter per second we will create Velocity which is the direction the player is moving in multiplied by the speed their moving in.

Create a variable that we can manipulate in the Inspector with a Type Float name speed. Assign it a value of 5f to move the player 5 meters per second. Also, create a Type Vector3 variable name velocity as well. Then inside of the Update() function, use the velocity variable to multiply the direction times speed for how fast the player moves in a given direction. With the Character Controller we can Move the player with the velocity multiplied by real time.

Velocity created with direction and speed applied.

Now when we play the game, notice the player moving faster than before. The player is now moving at a rate of 5 meters per second across the screen. However, the player can’t touch the ground because it has no gravity applied to them.

Player moving with velocity applied.

To fix this, we need to create a variable with a Type Float name gravity which will be assigned a value of 1f to represent the movement the player will fall down onscreen when gravity is applied.

Then inside the Update() function, we need to check IF the player which is represented by the controller IsGrounded. If the player IsGrounded, Do Nothing for now, but if the player is not grounded we will use the Velocity Y-axis and decrease it by the value of Gravity.

Gravity applied to the player if they’re in the air.

Play the game and the 1st thing we will see now is the Player falling down to the platform at a rate of one meter per second.

Player dropping from the sky due to gravity applied.

Next time we will work on the Player’s Jumping Behavior.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet