In a world of game development, we need to move, scale and rotate game objects million times. Even if we are not building a massive game, even if we are only building a simple and casual games, the movements of game objects are extraordinarily important. That reason alone is enough to tell us how importance transform.position is.

So open your Unity. Every time you click on inspector, you can see a clear Transform Position there

They pretty much show you the current condition of an object. Whenever you want to move an object, rotate or even rescale it, you can do it by changing the numbers there. Well of course you can always use the hotkey QWER.

However, sometimes we want to do that IN-GAME. Which means, we need to include that in our script. For starters, lets create a cube and insert a script there.

using UnityEngine;
using System.Collections;

public class cube : MonoBehaviour {

public float velocity = 3f;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
transform.Translate (velocity, 0, 0);
}
}

The function transform.translate basically moves an object in x, y and z. If we play that, you can see that the box is moving so fast that it looks like it disappears. What happens here?

the method Update() is a method in MonoBehavior that is called ONCE every frame. Basically code above tells Unity to move that cube by 3 in respect to X ONCE every frame.

The interesting stuff to know is, every computer can have various Frame per second (FPS). Some will have 60 fps, some will have 30. So, based on it we can conclude that :

On 30 FPS Computer              –> Cube moves 3 * 30 = 90 positions

On 60 FPS Computer              –> Cube moves 3 * 60 = 180 Positions

It is not only unfair, but also doesn’t make any sense at all. You can once again see that the object moves so fast that it looks as if it disappeared. Here is when a method called Time.deltaTime comes

So what does Time.deltaTime do ?  Time.deltaTime returns the time in seconds it took to complete the last frame. And how is it related to our case?

In 30 FPS Computer, one second can have 30 frames. Which means, 1 frame needs 1/30 second. In this case, Time.deltaTime will return 1/30. The same goes for 60 fps computer. Time.deltaTime will return 1/60 this time.

See that, we want our object to move 3 Positions / Second, not per frame because Second is universal while frame is not. But if we code translate(3,0,0) in Update() method, it will move per frame. This is where Time.deltaTime comes in handy

You see if we multiply 3 by 1/30 (which is Time.deltaTime) we get 3/30 in a frame. If a computer is 30 FPS and we have 3/30 position movements in a frame, we get 3 moves in a second, which is really perfect as we want it. The same goes on with 60 fps too! See, Time.deltaTime return 1/60 this time. If we multiply 3 with 1/60 we will get 3/60 per frame, and in one second we have 60 frames. Finally we also get 3 moves in a second, whether it is 30 FPS Computer of 60 FPS Computer.

So what do we do now?

Change

transform.Translate (velocity, 0, 0);

Into

transform.Translate (velocity * time.deltaTime, 0, 0);

And it will work just perfect. You can adjust it if you want the object to move in Y direction or Z. You can also adapt the same trick for rotations and scaling.

Author:

  • Yogi Udjaja
  • Houwen Lie