If you are a beginner Game Developer or Programmer in Unity, you must have come across the term Time.deltaTime. You might even know what it does, but have you ever wondered how it actually works?
Let’s break it down in a simple way.
🤔What is Time.deltaTime?
To understand Time.deltaTime, you first need to know what the Update() method is.
The Update method is the main player loop method in Unity. The Update() method is called every frame. Whatever tasks you define in the Update method will run every frame. One of the very first things you learn to do inside Update is to make your character move in the world.
Now let’s say you wrote some code that makes your character move 1 unit when you press a button. You save the code, Unity compiles it, and your character starts moving. Excited, you send the build to your friend who has a potato PC.
Now on his PC, the character moves slower and travels less distance than on your PC.
Why does this happen?
🔴The Real Problem: FPS
The issue here is Frames Per Second (FPS).
You have a powerful PC, so your game runs at a high FPS. This means your Update method gets called more times per second. So your character gets moved many more times.
Your friend’s PC is slower, giving fewer FPS. That means Update runs fewer times per second, so your character moves fewer times.
Result:
Higher FPS → Faster movement
Lower FPS → Slower movement
This is where Time.deltaTime comes to the rescue.
Time.deltaTime gives us the time (in seconds) passed since the last frame. When we multiply our movement with Time.deltaTime, it converts movement from per-frame movement to per-second movement.
So now, no matter how fast or slow the computer is, the character will travel the same distance in the same amount of real time.
⚙️How does it work?
Let’s understand it with numbers.
Let’s say:
- Your PC runs at 120 FPS
- Your friend’s PC runs at 60 FPS
Example 1: WITHOUT Time.deltaTime.
Movement each frame = 1 unit.
On 60 FPS machine:
- Update runs 60 times per second
- Movement per frame = 1 unit
- Movement per second = 60 × 1 = 60 units
On 120 FPS machine:
- Update runs 120 times per second
- Movement per frame = 1 unit
- Movement per second = 120 × 1 = 120 units.
Result: Faster machines move the character faster — BAD.
Example 2: WITH Time.deltaTime.
Movement each frame = 1 unit × Time.deltaTime
On 60 FPS machine:
- Time.deltaTime ≈ 1 / 60 = 0.01666 seconds
- Movement per frame = 1 × 0.01666 = 0.01666 units
- Movement after 60 frames (1 second): 0.01666 × 60 = 1 unit
On 120 FPS machine:
- Time.deltaTime ≈ 1 / 120 = 0.00833 seconds
- Movement per frame = 1 × 0.00833 = 0.00833 units
- Movement after 120 frames (1 second): 0.00833 × 120 = 1 unit.
Result: Consistent character movement, regardless of FPS.
🌠How to use it?
Using Time.deltaTime is very simple.
Just multiply your movement value with Time.deltaTime.
Take a look at following code snippet:
transform.Translate(Vector3.forward * 1f * Time.deltaTime);
// or
transform.position += movementSpeed * Time.deltaTime * transform.forward;
💡Key Takeaway
If you want to perform operations in the Update method and you want them to be frame-rate independent, then Time.deltaTime is your best buddy. Never do time-based movement in Update without it, unless you truly want different behavior on different machines.
💝Note
If you like what do, please hit a like, drop a comment and share this blog. This will help me stay motivated and bring you folks more interesting blogs like this. Thank You!
Top comments (0)