官方文档描述如下:
Description
The completion time in seconds since the last frame (Read Only).
This property provides the time between the current and previous frame.
Use Time.deltaTime to move a GameObject in the y
direction, at n
units per second. Multiply n
by Time.deltaTime and add to the y
component.
MonoBehaviour.FixedUpdate uses fixedDeltaTime instead of deltaTime. Do not rely on Time.deltaTime inside MonoBehaviour.OnGUI. Unity can call OnGUI multiple times per frame. The application use the same deltaTime value per call.
The following example implements a timer
. The timer
adds deltaTime each frame. The example announces the timer
value and resets it to zero when it reaches 2 seconds. The timer
does not hit 2.0 when MonoBehaviour.Update adds deltaTime. The test is for the timer
moving 2 seconds. The script code removes the reported time, and the timer
restarts. The restart is not always exactly 0.0. The speed changes between 0.5 and 2.0 seconds. Time.timeScale stores the chosen time-is-passing scale.
Unity官方给出的描述是:按照秒来计数,完成上一帧的时间(只读)。通常,使用这个函数来产生与游戏帧速率无关的效果。
如果你加上或者减去一个值,那你很可能应该乘以Time.deltaTime。当你乘以它以后,你实质上的表达是:我想让这个物体以每秒钟10米的速度移动而不是每帧10米。
当从MonoBehavior的FixedUpdate中调用的时候,返回的帧速率增量时间。
请注意,在OnGUI中你不应该依赖Time.deltaTime,因为OnGUI有可能在一帧中被多次调用并且每次deltatime的值都是相同的,直到下一帧刷新。
请看官方的示例代码:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Update() {
float translation = Time.deltaTime * 10;
transform.Translate(0, 0, translation);
}
}