Construct the Player Movement On Your Own- Game Dev Series 78

S.J. Jason Liu
4 min readJul 13, 2021

Objective: create a physics based character controller.

Oops…

In Galaxy Shooter series, we have created the basic 4-direction movement. In this platformer game, we will use a function in Unity called Character Controller. It is a component that allows user to control the gameobject (character) by using its properties and methods with code.

In this article, we will create the basic movement function to our Player.

Add components

First we need to add a character controller component to Player.

So far, we don’t need to adjust any value on it. Just keep it with default setting.

Next, create a script called “Player” and attach it to Player.

Open Player, and we will start creating the first function of it.

Horizontal movement

First we need to get the reference of character controller.

Then receive the horizontal movement value in Update() by using x-axis input.

To move the Player by using character controller, we need to use the method “Move”, which will need a vector3.
Create a new vector3 base on the horizontal value that we got.

And we also need to make the Player moves with velocity. Create a speed variable and use it with a new vector3 value.

Now we can move Player.

The Player should float in the air like this.
The next is to create gravity to Player.

Use gravity

Instead of using gravity from rigidbody, we will create our gravity in code. Create a new float variable as gravity value.

There is a property called “isGrounded” from character controller. It is used to check if the character is touched ground, which we will use it to determine when will the Player get effected by gravity.

Add an if-statement in Update() above the Move method.

In this if-statement, we will make sure that Player would only get gravity when it is in the air. To make this effect more obviously, let’s add the jumping function to our Player.

Jumping

Create another float variable of jumping, and add it to the if-statement.

Now we can try the jumping and how gravity effects the Player.

However, it become like this weird tiny jumping. Take a look at our script to find out where cause this issue.

Every frame it will calculate the movement(line: 4) and velocity(line: 5). When we jumped, the velocity.y should become 15. However, in the next frame, it become 0 as the movement(line: 4) calculate again. Our Player won’t even jump till the height we set and start effected by gravity to go down.

To fix this situation, we need another variable to storage the current value of velocity.y and replace the origin velocity.y every frame.

Then instead of set the velocity.y to jumping value, set _yVelocity to jumping value. And replace the velocity.y value to _yVelocity before the Move method.
That way, it would replace the value 0 every frame.

Play again and it should jump smoothly.

--

--

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.