当前位置:网站首页>Unity的脚本的基础语法(2)-Unity中记录时间
Unity的脚本的基础语法(2)-Unity中记录时间
2022-06-30 12:18:00 【ht_game】
在Unity中记录时间需要用到Time类。
Time类中比较重要的变量为deltaTime(只读)
deltaTime:指完成最后一帧所花费的时间(从最后一帧到当前帧的间隔(以秒为单位)),也即是增量时间
使用:如果想均匀地旋转一个物体,在不考虑帧速率的情况下,可以乘以Time.deltaTime。
//旋转
void Update () {
this.transform.Rotate(10 * Time.deltaTime, 0, 0);
}
//移动
void Update () {
this.transform.Translate(0, 0, 10 * Time.deltaTime);//每秒移动10米
}
如果想每秒增加或者减少一个值,需要乘以Time.deltaTime,同时也要明确在游戏中是需要每秒1个单位还是每帧1个单位的效果。如果是乘以Time.deltaTime,那么游戏对象就会按固定的节奏而不是依赖游戏的帧速率,因此,游戏对象的运动变得更容易控制。
为什么要用Time.deltaTime?
Update()每一秒更新的帧数是不一样的,增量时间是实时变动的,而每一帧都是变动的。增量时间保证了无论帧率是多少,都可以让物体移动定值。
1秒30帧,那么帧量时间就是1/30秒
1秒180帧,那么帧量时间就是1/180秒
总的来说,使用Time.deltaTime可以让物体移动或者旋转指定定值。比如,每秒移动N米
例如
public GameObject gameObject;
void Update () {
Vector3 te = gameObject.transform.position;//获取游戏对象的位置坐标
te.y += 5 * Time.deltaTime;//沿y轴每秒上升5个单位
gameObject.transform.position = te;//设置游戏对象的位置坐标
//这个方法其实和MovePOSiton方法相视
}
如果涉及刚体,可以写在FixedUpdate方法中。在FixedUpdate方法中,如果想每秒增加或者减少一个值,需要乘以Time.fixedDeltaTime。
例如
public GameObject gameObject;
void FixedUpdate()
{
Vector3 te = gameObject.GetComponent<Rigidbody>().transform.position;
te.y += 5 * Time.fixedDeltaTime;
gameObject.GetComponent<Rigidbody>().transform.position = te;
}
边栏推荐
- Building of Hisilicon 3559 universal platform: obtaining the modified code of data frame
- c# 怎样能写个sql的解析器
- JMeter性能测试之相关术语及性能测试通过标准
- 21. Notes on WPF binding
- Redis的配置文件及新数据类型
- How to solve cross domain problems
- Some commonly used hardware information of the server (constantly updated)
- Sqlserver query code is 936 simplified Chinese GBK. Should I write 936 or GBK?
- Instructions for legend use in SuperMap iclient3d 11i for cesium 3D scene
- Q-learning notes
猜你喜欢

How to use AI technology to optimize the independent station customer service system? Listen to the experts!

Instructions for legend use in SuperMap iclient3d 11i for cesium 3D scene

Basic interview questions for Software Test Engineers (required for fresh students and test dishes) the most basic interview questions

Idea has a new artifact, a set of code to adapt to multiple terminals!

Commands for redis basic operations

Today in history: Microsoft acquires PowerPoint developers; SGI and MIPS merge

MySQL built-in functions

New function of SuperMap iserver11i -- release and use of legend

【目标跟踪】|pytracking 配置 win 编译prroi_pool.pyd

SuperMap 3D SDKs_ Unity plug-in development - connect data services for SQL queries
随机推荐
QT implementation dynamic navigation bar
Substrate 源码追新导读: 质押额度大幅度削减, RocksDB可以完全被Disable
SuperMap 3D SDKs_ Unity plug-in development - connect data services for SQL queries
[MySQL] MySQL installation and configuration
时空预测2-GCN_LSTM
FFMpeg AVBufferPool 的理解与掌握
Clipboardjs - development learning summary 1
Tencent cloud Database Engineer competency certification was launched, and people from all walks of life talked about talent training problems
Commands for redis basic operations
Redis cache problem
Questionnaire star questionnaire packet capturing analysis
Building of Hisilicon 3559 universal platform: obtaining the modified code of data frame
60 个神级 VS Code 插件!!
Linux系统Redis的安装
Why should offline stores do new retail?
90. (cesium chapter) cesium high level listening events
Layout of pyqt5 interface and loading of resource files
"Xiaodeng" user personal data management in operation and maintenance
基于ThinkPHP5封装tronapi-波场接口-PHP版本--附接口文档-20220627
[QNX Hypervisor 2.2用户手册]6.2.3 Guest与外部之间通信