Rendering and script execution takes time. It differs every frame.
If you want ~60 fps, you will not have stable fps - but differing amounts,
of time passing each frame. You could wait if you are too fast,
but you cannot skip rendering when you are slower than expected.
To handle different lenghts of frames, you get the "Time.deltaTime".
In Update it will tell you how many seconds have passed
(usually a fraction like 0.00132) to finish the last frame.
If you now move an object from A to B by using this code:
object.transform.position += Vector3.forward * 0.05f;
It will move 0.05 Units per frame. After 100 frames it has moved 5 Units.
Some pcs may run this game at 60fps, others at 144 hz.
Plus the framerate is not constant.
So your object will take 1-5 seconds to move this distance.
But if you do this:
object.transform.position += Vector3.forward * 5f * Time.deltaTime;
it will move 5 units in 1 second. Independent of the framerate.
Because if you have 10000 fps the deltaTime will be very very small
so it only moves a very tiny bit each frame.
Note: In FixedUpdate() its technically not 100% the same every frame,
but you should act like it's always 0.02f or whatever you set the physics
interval to. So independent from the framerate,
the Time.deltaTime in FixedUpdate() will return the fixedDeltaTime
using UnityEngine;
// Rotate around the z axis at a constant speed
public class ConstantRotation : MonoBehaviour
{
public float degreesPerSecond = 2.0f;
void Update()
{
transform.Rotate(0, 0, degreesPerSecond * Time.deltaTime);
}
}
-- Simple Explanation for Time.deltaTime --
◉ Using Time.deltaTime Unity can tell us how long each frame took to execute
◉ When we multiply something by Time.deltaTime it makes our game
"frame rate independent "
◉ The game behaves the same with slow and fast computers
Here a small Calculation for the big brainers
◉◉ Slow PC ◉◉ ◉◉ Fast Pc ◉◉
Fps: 10 100
Duration
of Frame 0.1s 0.01s
Distance
per Second 1 x 10 x 0.1 = 1 1 x 100 x 0.01 = 1
Both, slow and fast pc, move at the same rate. Without deltatime (duration of Frame)
the fast pc would be mooving much faster.