Design a site like this with WordPress.com
Get started

Day 45: Time.deltaTime

I grew up playing on dedicated consoles like the Sega Genesis where a game would run exactly the same no matter whose Sega you played it on.

On PC, developing a game means having to adapt code to run smoothly on virtually an unlimited amount of computer configurations.

This is where the concept of Time.deltaTime comes in. (I’m just reiterating what I’ve learned to solidify the points in my head – feel free to correct me if my understanding on any of the points are wrong)

Let’s say I’m creating a game where I want to move an object at the speed of 6:

public class moveObject : MonoBehaviour
{
     public float speed = 6f;
      
     void Update()
     {
         transform.Translate (speed, 0, 0)
     }

But I’m poor and have a slow computer. It runs the game at 30 frames per second.

FPS (30) * speed (6) = 180 (distance moved)

Someone else has a faster computer. It plays at 55 frames per second.

FPS (55) * speed (6) = 330 (distance moved)

In one second, the game object on the faster computer traveled nearly twice as far in the same time because it called the Update() function 25 more times in one second.

Time.deltaTime is used make this movement frame independent. It represents the time passed in seconds since the last Update().

public class moveObject : MonoBehaviour
{
     public float speed = 6f;
      
     void Update()
     {
         transform.Translate (speed * Time.deltaTime, 0, 0)
     }

My Slow Computer
30 (frames) / 1 (second) = 0.0333333333333333 (time between updates)
Speed * Time.deltaTime =
6 * 0.0333333333333333 = 0.2 (amount moved each frame)
30 FPS * 0.2 = 6 (distance moved each second)

Someone’s Faster Computer
55 (frames) / 1 (second) = 0.0181818181818182
Speed * Time.deltaTime =
6 * 0.0181818181818182 = 0.109090909909091
55 FPS * 0.090909099090909 = 6 (distance moved each second)

Using Time.deltaTime made the distance traveled by the object frame independent. Now no matter how fast or slow your computer runs, it will move at the same rate on screen as another player’s.

The same principle applies on the same computer. If I have a lot of programs running, my game might be running slower at some points and faster at others. Time.deltaTime will smooth the movement of the object instead of it looking twitchy each time Update() is called and the object ‘rushes’ to catch up.

In the same vein of frame rate independency, I also need to learn about FixedUpdate() for physics calculations.

Current Progress Status

  • Tower Defense Tutorial: 47/73
  • Unity in Action (Pg 324 of 352)

Onward!

Advertisement

Author: Mike@PAUSED

Aspiring indie game developer. Devlog: www.paused.ca

One thought on “Day 45: Time.deltaTime”

  1. Yea, timing has always been a mess. However, the most nice thnig is that there’s quite NOTHING really explanative in the official documentation. Just like back in those days, where you had to find out all features and tricks of the game by yourself just to progress through the level. See, how the Unity Team cares about those back-in-the-day-hardcore-dudes? They challange you with every line of code. Yes, that’s the point, hardcore or nothing.
    Just joking, anyway.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: