当前位置:网站首页>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;
}
边栏推荐
- Mysql中 begin..end使用遇到的坑
- 两批次纯牛奶不合格?麦趣尔回应:正对产品大批量排查抽检
- Flink sql控制台,不识别group_concat函数吗?
- Visual Studio配置Qt并通过NSIS实现项目打包
- Pharmacy management system
- JMeter's performance test process and performance test focus
- kubeedge的核心理念
- FFMpeg AVBufferPool 的理解与掌握
- MySQL判断执行条件为NULL时,返回0,出错问题解决 Incorrect parameter count in the call to native function ‘ISNULL‘,
- 【一天学awk】正则匹配
猜你喜欢

Vision based robot grasping: from object localization, object pose estimation to parallel gripper grasping estimation

21. Notes on WPF binding

【一天学awk】数组的使用

腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?

问卷星问卷抓包分析

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

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

【一天学awk】内置变量的使用

Redis-緩存問題

Google refutes rumors and gives up tensorflow. It's still alive!
随机推荐
[one day learning awk] Fundamentals
【目标跟踪】|pytracking 配置 win 编译prroi_pool.pyd
Hisilicon 3559 developing common sense reserves: a complete explanation of related terms
How to use the plug-in mechanism to gracefully encapsulate your request hook
解决numpy.core._exceptions.UFuncTypeError: ufunc ‘add‘ did not contain a loop with signature matchin问题
RDS MySQL数据迁移PolarDB MySQL费用可以转过去吗?
Pharmacy management system
ffmpeg 杂项
Iserver publishing es service query setting maximum return quantity
How to use AI technology to optimize the independent station customer service system? Listen to the experts!
Browser plays RTSP video based on nodejs
Redis - problèmes de cache
Four ways for flinksql to customize udtf
How to select an OLAP database engine?
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年6月29日21:59:34
SuperMap 3D SDKs_ Unity plug-in development - connect data services for SQL queries
JMeter性能测试工作中遇到的问题及剖析,你遇到了几个?
[surprised] the download speed of Xunlei is not as fast as that of the virtual machine
Vision based robot grasping: from object localization, object pose estimation to parallel gripper grasping estimation
MySQL中变量的定义和变量的赋值使用