学习随记——Time.deltaTime的含义及其应用

官方文档描述如下:

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值