查看: 1261|回复: 1
打印 上一主题 下一主题

[其他] Overview: Keeping Track of Time 记录时间

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2012-10-11 10:18:11 |只看该作者 |倒序浏览
The Time class contains a very important class variable called deltaTime. This variable contains the amount of time since the last call to Update or FixedUpdate (depending on whether you are inside an Update or a FixedUpdate function).
So the previous example, modified to make the object rotate with a constant speed not dependent on the frame rate becomes:
Time 类包含一个非常重要的变量叫deltaTime.这个变量包含从上次调用Update 或FixedUpdate到现在的时间(根据你是放在Update函数还是FixedUpdate函数中).(另注: Update每帧调用一次)
依照上面的例子,使物体在一个均匀的速度下旋转,不依赖帧的频率,如下:


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {    void Update() {        transform.Rotate(0, 5 * Time.deltaTime, 0);    }}


function Update() {    transform.Rotate(0, 5 * Time.deltaTime, 0);}


Moving the object: 移动物体:


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {    void Update() {        transform.Translate(0, 0, 2 * Time.deltaTime);    }}


function Update() {    transform.Translate(0, 0, 2 * Time.deltaTime);}


If you add or subtract to a value every frame chances are you should multiply with Time.deltaTime. When you multiply with Time.deltaTime you essentially express: I want to move this object 10 meters per second instead of 10 meters per frame. This is not only good because your game will***n the same independent of the frame rate but also because the units used for the motion are easy to understand. (10 meters per second)
Another example, if you want to increase the range of a light over time. The following expresses, change the radius by 2 units per second.
如果你每帧添加或减少一个值,你需要乘以 Time.deltaTime .在你乘以 Time.deltaTime 的同时你必须明确:你是以每秒10米而不是每帧10米来移动物体.这样你的游戏就会以自己的节奏来运行而不是依赖帧的频率,因而也使游戏中的运动更容易掌握.(每秒10米)
另外一个例子,如果你想要每隔一段时间增加光照范围.下面的代码实现的是每秒增加半径2个单位.


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {    void Update() {        light.range += 2.0F * Time.deltaTime;    }}


function Update() {    light.range += 2.0 * Time.deltaTime;}


When dealing with rigidbodies through forces, you generally don't multiply by Time.deltaTime since the engine already takes care of that for you.
当涉及到作用于刚体的力时,通常不乘以 Time.deltaTime .因为引擎已经为你处理了.



来源:unity3d圣典 更多3D资讯尽在web3D纳金网http://www.narkii.com/



————————————————————————————————


Subsections 章节

     [脚本章节]Scripting Overview 脚本概述
    [脚本章节]Overview: Script compilation 脚本编译(高级)
    [脚本章节]Overview: Performance Optimization 性能优化
    [脚本章节]Overview: The most important classes 重要的类
    [脚本章节]Overview : Writing Scripts in C# 使用C#书写脚本
    [脚本章节]Overview: Coroutines & Yield 协同程序 & 中断
    [脚本章节]Overview: Instantiate 实例
    [脚本章节]Overview: Member Variables & Global Variables 成员变量 & 全局变量
    [脚本章节]Overview: Vectors 向量
    [脚本章节]Overview: Accessing Other Game Objects 访问其他游戏物体
    [脚本章节]Overview: Accessing Other Components 访问其他组件
    [脚本章节]Overview: Keeping Track of Time 记录时间
    [脚本章节]Overview: Common Operations 常用操作
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

0

主题

1

听众

758

积分

初级设计师

Rank: 3Rank: 3

纳金币
8
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-8-14 18:06:01 |只看该作者
thanks for sharing
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-20 17:39 , Processed in 0.108937 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部