Simple Player Movement in Unity — Part 2

Pete Thomasberger
5 min readApr 11, 2021

--

In the last article, we looked at creating a GameObject and instantly transporting the GameObject from one position to another position on our screen. In this article, we will be looking at moving the object at variable speeds.

To move a game object in any direction in Unity, we need to use the Translate function within our C# script. So, to move our Player GameObject to the right indefinitely, we would use the following code within the Update method:

transform.Translate(new Vector3(1, 0, 0));

or

transform.Translate(Vector3.right);

(This is the equivalent code shortcut of transform.Translate(new Vector3(1, 0, 0);)

Let’s break these code lines down.

One note about GameObjects in Unity is that to access any GameObject properties within the Inspector window, such as Position, we must first access the Transform of the GameObject. Therefore, within our script, a GameObject is a Transform, and Transform is a GameObject.

Knowing this, in our line of code, we use transform first because we want to move (translate) the GameObject. Then we tell the script where we want to go, which is to the right, so we access the X-axis and say Translate(new Vector3(1, 0, 0)) or Translate(Vector3.Right). Alternatively, if we wanted to go left, we would use Translate(new Vector3(-1, 0, 0)), or Translate(Vector3.left). If we wanted to move our GameObject up, we would need to use the Y-axis and say Translate(new Vector3(0, 1, 0)) or Translate(Vector3.up). Again, if we wanted to go down, we would say Translate(new Vector3(0, -1, 0)) or Translate(Vector3.down).

Also, when we use a value of 1 or -1 within our Vector3 when utilizing the Translate function, we are talking about meters per frame and not the position on the screen. So, if we put our code within the Start method, our GameObject would only move to the right 1 meter and stop. But by putting the code within the Update method, which is called once per frame, we will be moving at a rate of 1 meter per frame and essentially 60 frames per second until we stop running the script.

To sum all this info up, our code is saying we want the Transform (GameObject) to continuously move to the right at a rate of 1 meter per frame.

Now, 1 meter per frame is extremely fast and we most likely will want to slow this speed down by changing the movement rate from meters per frame to meters per second. To do this, we will utilize Time.deltaTime within our code. Time.deltaTime is the equivalent of 1 second in real-time and we can multiply this by a value to create any speed.

For example:

transform.Translate(Vector3.right * Time.deltaTime); is saying we want to move the Transform (GameObject) to the right at a rate of 1 meter per second.

But if we wanted to move the GameObject to the right 5 meters per second, we would use transform.Translate(Vector3.right * 5 * Time.deltaTime);

We can also make it to where we can assign the speed multiplier within Unity by creating either a public variable or a private variable (utilizing [SerializeField]) for the speed before the Start method. For this example, we will create a private variable called _speed (using “_” to prefix the variable name indicates that the variable is a private variable). On its own, a private variable will not be adjustable within Unity unless we use [SerializeField] to make it visible in the Inspector. Also, we will need to use a float variable, which requires an “f” to be added after the number value, as we may want to fine-tune the speed value in smaller increments than a whole number.

So, here is how our declared variable code will look:

When we save this code and go back into Unity and look at our Player script component in the Player GameObject Inspector window, we will see our _speed variable is now visible and ready to be modified.

But currently, our _speed variable is not being utilized in our Player movement line of code, so adjusting Speed in Unity will not yield any results. To implement the _speed variable in our Player movement code, we will replace our multiplier value with the _speed variable, which looks like this:

transform.Translate(Vector3.right * _speed * Time.deltaTime);

Now when we save our code and go back into Unity, we will be able to adjust the rate of speed for our cube.

In the next article, we will look at getting user input to control the direction of movement for our GameObject.

Dev Day 10: Simple Player Movement in Unity — Part 1

Dev Day 12: Simple Player Movement in Unity — Part 3

--

--