Player Movement- Game Dev Series 03

S.J. Jason Liu
Nerd For Tech
Published in
7 min readApr 28, 2021

--

To move in a 3D world, you need Vector 3.

Previous: Make a Efficient Layout

Once we open a new project, we will automatically get 2 game object:
Main camera & Directional light.

We need to add a game object as our player to move.

To create, right click on the Hierarchy > 3D Object > Cube.
Or you can click the GameObject drop down menu to create.

Right click on Hierarchy
Click from drop down menu

We can see the game object is on our Game view.

Let’s change the name to Player, and now we are going to make it move.

Every game object in Unity has a Transform component which contains 3 data: Position, Rotation, and Scale.

If we want to move a game object, we will need to control the Position.

First, let’s drag our Player on the Y axis to about negative 2 as our default position.

Then we create a script folder in the Project window and create a C# script named Player.

Make sure you name the script at the beginning or you will need to change both script name and class name inside the script editor later.

Let’s drag the script to Player game object and open the script by double click it in the Inspector window.

To get the current position, we need to access the transform component insides void Start():

void Start()
{
transform.position
}

Then we can set our new position after an equal mark.
To set a new position in 3D, we use Vector3 and always add new in front of it .

void Start()
{
transform.position = new Vector3()
}

By input Vector3, you can use up & down arrow key to look for the references.

Now we want to make our Player back to 0 on the Y axis, we can make the change in the Vector3:

void Start()
{
transform.position = new Vector3(0, 0, 0);
}

We make it all 0 on the X, Y, and Z axis. And always add a semicolon at the end.

Save our script and back to Unity then click Play on the top middle.

The Player go to 0, 0, 0 when plays. That’s how Start() function works.
It works when the script got called and before the first frame update.

Now let’s move our Player constantly.

You might notice that there is another function called Update() under the Start().
Unlike Start(), Update() works every frame. We can give the order inside Update() to make it action in every frame.

To move our position, we need another method called Translate.

void Update()
{
transform.Translate();
}

Then we need to tell it which direction we want to move.
Let’s make it move to left. We can access the movement simply input Vector3.left.

void Update()
{
transform.Translate(Vector3.left);
}

Save script and Play in the Unity to see the result.

You can see our Player flies far away immediately like a bullet.
That is because it move 1 unity unit(almost 1 meter) per frame. Normally there are 60 frames in 1 second in our game. When you got a better equipment, it become higher. Which means it flies at least 60 meters per second after started.

We can change the speed by input the time variable:

void Update()
{
transform.Translate(Vector3.left * Time.deltaTime);
}

Time.deltaTime is a variable that allows you to run in every second. And with the asterisk to multiply, it makes our movement run 1 meter per second, not frame.
Here is the result:

Now we are able to move Player normally, we can add an input control.

Inside Unity, there is a default input method. We can examine it by the Project Settings drop down menu> Input Manager > Axes, where you can see all the input default setting by Unity.

By drop down Horizontal and Vertical, there are default button for both.

We can make a simple test to understand what value would it be if we input these buttons.
Let’s set 2 public variables for horizontal and vertical input.

public float horizontalInput;
public float verticalInput;
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
}

we can access our input by using Input.GetAxis.

Notice that we need to input the string is exactly the same as it shows on the Input Manager with capital. Unity will read as an input.
Hit Play and press WSAD or arrow keys to see the result:

When press A&D, horizontal will get input from -1 to 1, and also for vertical when press W&S.
These are input value we need to adding to our player.

Multiplying horizontalInput as horizontal movement. And add another line for vertical movement by using Vector3.up & verticalInput.

Play it and you should be able to control your Player now.

It works! However, the move speed is too slow. We need to add an adjustable value to increase the speed.
Add a private variable so the player speed won’t be changed by other script:

To verify a private variable inside a script, use underscore at the beginning of the name.

private float _speed = 3.5f;

Then we can multiply _speed to our movement:

Now our player moves more reasonable.

Although our player can move smoothly, there is one problem left, we need to restrict the moving space.

We can check the value in the Inspector by dragging our player.

As the result, we should restrict our Player’s position from -3.8 to 0 on Y-axis, and -11.3 to 11.3 on X-axis.
Now we can add our restrict in script.

Use new Vector3 to se a new position. So far, we just want to set our Y-axis, we can keep our X-axis as current position by using transform.position.x.
On the Y-axis, we can use Mathf.Clamp to manage a range as our minimum and maximum value.
In Mathf.Clamp, you need to input 3 values:

  • 1- float you want to restrict, which is the position-y.
  • 2- the minimum value, which is -3.8.
  • 3- the maximum value is 0.
transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);

Then the player won’t be disappear on Y-axis.

As position-x, we can make it disappear when reach the edge of both sides and shows up on the opposite.
In that case, we need if-statement. An if-statement allows us to make a statement works if the specific condition happened,

if(//this happened)
{
//doing this
}

If our position-x reach 11.3 or -11.3, we should make the value negative. This would make the position-x value opposite when it happened.
This time, we should keep transform.position.y as the same.

In the condition part, there is a || which is used as “or”. That means if x is greater than 11.3f or less than -11.3f, then set the new position.
Then we add a negative mark at the beginning of transform.position.x.
Here’s how it works,

That’s all for the player movement.

--

--

S.J. Jason Liu
Nerd For Tech

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