0
点赞
收藏
分享

微信扫一扫

【Unity3D】Unity 游戏画面帧更新 ( 游戏物体 GameObject 移动 | 借助 Time.deltaTime 进行匀速运动 )


文章目录

  • ​​一、 游戏物体 GameObject 移动​​
  • ​​二、 借助 Time.deltaTime 进行匀速运动​​






一、 游戏物体 GameObject 移动


在 Unity 中 , 如果想要让 游戏物体 GameObject 移动 , 则需要在 MonoBehaviour#Update() 函数 中 , 不断的修改 物体的 Transform#localPosition 坐标位置 ;



在 MonoBehaviour#Start() 函数 中 , 先 设置游戏的帧率

// 设置游戏更新帧率 50 fps
Application.targetFrameRate = 50;



在 MonoBehaviour#Update() 函数 中 , 进行如下画面更新操作 , 每次更新画面帧时 , 计算 游戏场景 中的 游戏物体 的运行位置 , 然后设置给游戏物体 ;



首先 , 获取当前 游戏物体 GameObject 的本地坐标 , 赋值给 Vector3 类型的变量 ;

// 获取 物体的 当前位置 本地坐标
Vector3 localPosition = this.transform.localPosition;

然后 , 将 坐标的 x 分量自增 0.02f

// 坐标的 x 分量自增 0.02f , 之前设置游戏帧率 50fps, 也就是每秒移动 1 米距离
localPosition.x += 0.02f;

最后 , 将修改后的坐标设置回去 ;

// 将坐标设置回去 , 更新物体的位置
this.transform.localPosition = localPosition;



完整代码如下 :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// 打印日志
Debug.Log("Unity 脚本入口 , 启动加载时调用");

// 设置游戏更新帧率 50 fps
Application.targetFrameRate = 50;

// 获取当前组件附着的 游戏物体 GameObject
GameObject gameObject = this.gameObject;

// 获取当前组件附着的 游戏物体 GameObject 名称
string name = gameObject.name;
Debug.Log("C# 脚本附着游戏物体的名称 : " + name);

// 获取当前组件附着的 游戏物体 GameObject 的 Transform 组件
Transform transform = gameObject.transform;

// 获取 Transform 组件的 位置 , 旋转量 , 缩放倍数
Debug.Log("C# 脚本附着游戏物体的 Transform 组件数据 位置 : " + transform.position
+ " , 旋转量 : " + transform.rotation + " , 缩放倍数 : " + transform.localScale);

// 将 当前组件附着的 游戏物体 GameObject 移动到 (4.0f, 4.0f, 4.0f) 坐标位置
//this.transform.localPosition = new Vector3(4.0f, 4.0f, 4.0f);

}

// Update is called once per frame
void Update()
{
Debug.Log("C# 脚本 Update 函数调用 , 游戏帧更新 , 当前游戏时间 : " + Time.time + " , 本次更新距离上次更新时间差 : " + Time.deltaTime);

// 将 当前组件附着的 游戏物体 GameObject 沿 X 轴方向移动
// 获取 物体的 当前位置 本地坐标
Vector3 localPosition = this.transform.localPosition;
// 坐标的 x 分量自增 0.02f , 之前设置游戏帧率 50fps, 也就是每秒移动 1 米距离
localPosition.x += 0.02f;
// 将坐标设置回去 , 更新物体的位置
this.transform.localPosition = localPosition;
}
}

运行效果 :

初始状态 :

【Unity3D】Unity 游戏画面帧更新 ( 游戏物体 GameObject 移动 | 借助 Time.deltaTime 进行匀速运动 )_FPS

运行一段时间后 :

【Unity3D】Unity 游戏画面帧更新 ( 游戏物体 GameObject 移动 | 借助 Time.deltaTime 进行匀速运动 )_帧率_02






二、 借助 Time.deltaTime 进行匀速运动


上述游戏物体运动 , 不是匀速运动 , 每次在 MonoBehaviour#Update() 函数 中 , 累加一个固定值 , 但是 该函数调用的间隔不是固定的

如果将该运动设置为匀速运动 , 可以 设置一个固定的速度值 , 根据 通过 ​​Time.deltaTime​​ 代码 获取的 本次更新与上一次更新的时间差 , 计算出本次应该移动多少距离 ;

将固定速度值设为 1 米 / 秒 ;

完整代码如下 :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// 打印日志
Debug.Log("Unity 脚本入口 , 启动加载时调用");

// 设置游戏更新帧率 50 fps
Application.targetFrameRate = 50;

// 获取当前组件附着的 游戏物体 GameObject
GameObject gameObject = this.gameObject;

// 获取当前组件附着的 游戏物体 GameObject 名称
string name = gameObject.name;
Debug.Log("C# 脚本附着游戏物体的名称 : " + name);

// 获取当前组件附着的 游戏物体 GameObject 的 Transform 组件
Transform transform = gameObject.transform;

// 获取 Transform 组件的 位置 , 旋转量 , 缩放倍数
Debug.Log("C# 脚本附着游戏物体的 Transform 组件数据 位置 : " + transform.position
+ " , 旋转量 : " + transform.rotation + " , 缩放倍数 : " + transform.localScale);

// 将 当前组件附着的 游戏物体 GameObject 移动到 (4.0f, 4.0f, 4.0f) 坐标位置
//this.transform.localPosition = new Vector3(4.0f, 4.0f, 4.0f);

}

// Update is called once per frame
void Update()
{
Debug.Log("C# 脚本 Update 函数调用 , 游戏帧更新 , 当前游戏时间 : " + Time.time + " , 本次更新距离上次更新时间差 : " + Time.deltaTime);

// 将 当前组件附着的 游戏物体 GameObject 沿 X 轴方向移动
// 获取 物体的 当前位置 本地坐标
Vector3 localPosition = this.transform.localPosition;
// 计算移动的距离
// 速度设置为 1 单位 / 秒
float speed = 1f;
// 计算长度 , 速度 乘以 距离上次帧更新的时间差
float distance = speed * Time.deltaTime;
// 匀速运动值
localPosition.x += distance;
// 将坐标设置回去 , 更新物体的位置
this.transform.localPosition = localPosition;
}
}

初始状态 :

【Unity3D】Unity 游戏画面帧更新 ( 游戏物体 GameObject 移动 | 借助 Time.deltaTime 进行匀速运动 )_FPS

运行一段时间后 :

【Unity3D】Unity 游戏画面帧更新 ( 游戏物体 GameObject 移动 | 借助 Time.deltaTime 进行匀速运动 )_帧率_02


举报

相关推荐

0 条评论