当前位置:网站首页>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;
}
边栏推荐
- Qt中的数据库使用
- QT MSVC installation and commissioning
- 浅谈 JMeter 运行原理
- 【一天学awk】内置变量的使用
- [MySQL] MySQL installation and configuration
- SuperMap iclient3d for webgl loading TMS tiles
- Spatiotemporal prediction 2-gcn_ LSTM
- [QNX Hypervisor 2.2用户手册]6.2.3 Guest与外部之间通信
- Tencent two sides: @bean and @component are used on the same class. What happens?
- rpm2rpm 打包步骤
猜你喜欢
Shell基础入门
FlinkSQL自定义UDAF使用的三种方式
QT MSVC installation and commissioning
rpm2rpm 打包步骤
Visual Studio配置Qt并通过NSIS实现项目打包
Sarsa notes
After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms
【目标跟踪】|pytracking 配置 win 编译prroi_pool.pyd
黑马笔记---集合(Collection的常用方法与遍历方式)
MySQL composite query
随机推荐
论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》
MySQL中变量的定义和变量的赋值使用
【驚了】迅雷下載速度竟然比不上虛擬機中的下載速度
Building of Hisilicon 3559 universal platform: obtaining the modified code of data frame
Reading the table data of Tencent documents in the applet
[target tracking] |pytracking configuring win to compile prroi_ pool. pyd
The realization of QT the flipping effect of QQ weather forecast window
Generate entity classes from SQL Server database tables through EF core framework
MySQL判断执行条件为NULL时,返回0,出错问题解决 Incorrect parameter count in the call to native function ‘ISNULL‘,
Substrate 源码追新导读: Call调用索引化, 存储层事物化全面完成
MySql实现两个查询结果相除
Sword finger offer 05 Replace spaces: replace each space in the string s with "%20"“
Questionnaire star questionnaire packet capturing analysis
【一天学awk】数组的使用
【目标跟踪】|pytracking 配置 win 编译prroi_pool.pyd
Substrate 源码追新导读: 波卡系波卡权重计算全面更新, Governance 2.0 版本的优化和调整
Introduction to sub source code updating: mid May: uniques NFT module and nomination pool
Qt中的数据库使用
Mysql中 begin..end使用遇到的坑
Flink SQL console, group not recognized_ Concat function?