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






    一、 游戏物体 GameObject 移动



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


    MonoBehaviour#Start() 函数 中 , 先 设置游戏的帧率 , 为了方便计算 , 这里设置 50 fps ;

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

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


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

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

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

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

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

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

    完整代码如下 :

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    运行效果 :

    初始状态 :
    在这里插入图片描述

    运行一段时间后 :

    在这里插入图片描述





    二、 借助 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    初始状态 :
    在这里插入图片描述

    运行一段时间后 :

    在这里插入图片描述

  • 相关阅读:
    清华大学有多重视体育课,“无体育·不清华”不仅仅是口号
    conda常用命令
    异形双柱体阵列纳米粒:针状/花状纳米粒子/纳米金星/金笼/金壳/三角形纳米金
    Docker入门,Docker是什么?有什么用?该怎么用?
    智能井盖监测系统功能,万宾科技传感器效果
    Python框架篇(1):FastApi-快速入门
    (一) SpringCloud+Security+Oauth2微服务授权初步认识
    C#:实现计算两个点之间的球面距离算法(附完整源码)
    QT---lineEdit相关信号
    麒麟v10安装Redis(ARM架构)
  • 原文地址:https://blog.csdn.net/han1202012/article/details/127924826