Simple Player Movement in Unity

Currently standing still…
transform.Translate(Vector3.right * Time.deltaTime);

It’s insane to think that with this one line of code, you can get an object to start moving (in this case, to the right) in Unity.

It comes ALIVE!

Let’s break the line of code down to help determine why the object can move:

transform.Translate(Vector3.right * Time.deltaTime);
  • Transform is the object that we want to move. Therefore, the transform in this example is the cube
  • Translate is one of the many public methods that can be used when calling on the transform. In this case, translate moves the object in the direction and distance of the translation.
  • Vector3 is the representation of the transform’s position in a 3D world environment. It’s made up of 3 numbers, representing the X,Y,Z coordinates. Using Vector3.right is a shortcut of saying
new Vector3(1,0,0)
  • Time.deltaTime represents the interval from the last frame to the current one in seconds. As the Update method in Unity is called once per frame (usually 60fps), without adding it means the object will be moving 60 m/s. By multiplying this with the Vector3, it slows down the object to 1 m/s.

Using all these information, along with additional research, means you can turn a cube that moves right all the time to

Moving and stopping