当前位置:网站首页>Basic syntax of unity script (2) -record time in unity
Basic syntax of unity script (2) -record time in unity
2022-06-30 13:01:00 【ht_ game】
stay Unity It takes to record time in Time class .
Time The more important variables in the class are deltaTime( read-only )
deltaTime: The time taken to complete the last frame ( The interval from the last frame to the current frame ( In seconds )), That is, incremental time
Use : If you want to rotate an object evenly , Regardless of the frame rate , It can be multiplied by Time.deltaTime.
// rotate
void Update () {
this.transform.Rotate(10 * Time.deltaTime, 0, 0);
}
// Move
void Update () {
this.transform.Translate(0, 0, 10 * Time.deltaTime);// Move... Every second 10 rice
}
If you want to increase or decrease one value per second , need multiply Time.deltaTime, At the same time, it should also be clear that in the game, it takes every second 1 Units per frame 1 Effect of units . If it is multiplied by Time.deltaTime, Then the game object will follow a fixed rhythm instead of depending on the frame rate of the game , therefore , The motion of the game object becomes easier to control .
Why use Time.deltaTime?
Update() The number of frames updated per second is different , Incremental time changes in real time , And every frame changes . The incremental time guarantees that no matter what the frame rate , Can make the object move to a fixed value .
1 second 30 frame , So the frame size time is 1/30 second
1 second 180 frame , So the frame size time is 1/180 second
in general , Use Time.deltaTime The object can be moved or rotated to a specified value . such as , Move... Every second N rice
for example
public GameObject gameObject;
void Update () {
Vector3 te = gameObject.transform.position;// Get the position coordinates of the game object
te.y += 5 * Time.deltaTime;// Along the y The axis rises every second 5 A unit of
gameObject.transform.position = te;// Set the position coordinates of the game object
// This method is actually the same as MovePOSiton Method phase view
}
If rigid bodies are involved , It can be written in FixedUpdate In the method . stay FixedUpdate In the method , If you want to increase or decrease one value per second , It needs to be multiplied by Time.fixedDeltaTime.
for example
public GameObject gameObject;
void FixedUpdate()
{
Vector3 te = gameObject.GetComponent<Rigidbody>().transform.position;
te.y += 5 * Time.fixedDeltaTime;
gameObject.GetComponent<Rigidbody>().transform.position = te;
}
边栏推荐
- After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms
- Unity脚本的基础语法(4)-访问其他游戏对象
- 深度长文探讨Join运算的简化和提速
- postman 自动生成 curl 代码片段
- ffmpeg 杂项
- The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
- 黑马笔记---集合(Collection的常用方法与遍历方式)
- golang基础 —— 切片和数组的区别
- 力扣之螺旋矩阵,一起旋转起来(都能看懂)
- How to use AI technology to optimize the independent station customer service system? Listen to the experts!
猜你喜欢
排查问题的方法论(适用于任何多方合作中产生的问题排查)
Motor control Clarke( α/β) Derivation of equal amplitude transformation
资源变现小程序开通流量主教程
RK356x U-Boot研究所(命令篇)3.2 help命令的用法
Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen
Machine learning notes - Introduction to autocorrelation and partial autocorrelation
【驚了】迅雷下載速度竟然比不上虛擬機中的下載速度
深度长文探讨Join运算的简化和提速
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(二)
WTM重大更新,多租户和单点登录
随机推荐
一次 Keepalived 高可用的事故,让我重学了一遍它!
Package tronapi wave field interface based on thinkphp5 PHP version -- interface document attached -20220627
Unity脚本的基础语法(3)-访问游戏对象组件
顺应媒体融合趋势,中科闻歌携手美摄打造数智媒宣
App wechat payment unicloud version of uniapp payment (with source code)
JS 二维数组变一维数组的方法
kubeedge的核心理念
项目中遇到一个有趣的事情
Derivation of Park transformation formula for motor control
[one day learning awk] use of built-in variables
IDEA 2021.3 执行 golang 报错:RNING: undefined behavior version of Delve is too old for Go version 1.18
elementui中清除tinymce富文本缓存
问卷星问卷抓包分析
黑马笔记---常用日期API
RK356x U-Boot研究所(命令篇)3.2 help命令的用法
微信小程序报错:TypeError: Cannot read property ‘setData‘ of undefined
JMeter learning notes
Resource realization applet opening wechat official small store tutorial
Substrate 源码追新导读: 质押额度大幅度削减, RocksDB可以完全被Disable
Substrate 源码追新导读: 修复BEEFY的gossip引擎内存泄漏问题, 智能合约删除队列优化