zoukankan      html  css  js  c++  java
  • [Unity]Update()与FixedUpdate()

    Update()介绍


    首先我们从官方文档的介绍了解:

    MonoBehaviour.Update()


    • Description

    Update is called every frame, if the MonoBehaviour is enabled. Update is the most commonly used function to implement anykind of game behaviour.
    using UnityEngine;
    using System.Collections;
    
    public class ExampleClass : MonoBehaviour {
        void Update() {
            transform.Translate(0, 0, Time.deltaTime * 1);
        }
    }

    In order to get the elapsed time since last call to Update, use Time.deltaTime. This function is only called if the Behaviour is enabled. Override this function in order to provide your component’s functionality.

    FixedUpdate()介绍



    • Description

    This function is called every fixed framerate frame, if the MonoBehaviour is enabled. FixedUpdate should be used instead of Update when dealing with Rigidbody. For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of everyframe inside Update.
    using UnityEngine;
    using System.Collections;
    
    public class ExampleClass : MonoBehaviour
    {
        public Rigidbody rb;
    
        void Start()
        {
            rb = GetComponent<Rigidbody>();
        }
    
        void FixedUpdate()
        {
            rb.AddForce(10.0f * Vector3.up);
        }
    }

    In order to get the elapsed time since last call to Update, use Time.deltaTime This function is only called if the Behaviour is enabled. Override this function in order to provide your component’s functionality.

    二者区别


    从上面的介绍很显然可以了解到:fixedupdate的调用时间是固定的,与帧数无关,而update与当前平台的帧数有关,也就是说如果因为你的机器性能有所差异,表现出来的游戏帧数不同,那么update()的调用时间间隔也会不一样,而fixedupdate()的调用时间却是相同的,并且从上面的官方手册中我们可以了解到,如果要对物理刚体进行处理,应该调用的是fixupdate()



    附上youtube二者的官方教程视频:
    https://www.youtube.com/watch?v=ZukcUv3pyXQ

    https://github.com/li-zheng-hao
  • 相关阅读:
    vue 自定义全局按键修饰符
    Vue 过滤器
    v-if、v-show 指令
    其他内置函数
    python中序列化和反序列化
    jmeter图形化html报告核心指标介绍
    jmeter在linux系统下如何进行压力测试
    文件操作的其他方法
    文件处理操作
    内置函数reduce()
  • 原文地址:https://www.cnblogs.com/lizhenghao126/p/11053693.html
Copyright © 2011-2022 走看看