📜  unity time.deltatime 含义 - 无论代码示例

📅  最后修改于: 2022-03-11 15:00:44.482000             🧑  作者: Mango

代码示例1
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