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
  • 相关阅读:
    Hibernate表中多对多关系的映射
    Servlet JDBC工具类
    访问gridview中的各类控件
    ASP.NET2.0中Gridview中的内容导出到Excel
    List<T> 分页方式,泛型分页方式
    VS保存和编译问题] 总是出现另一个程序正在使用此文件,进程无法访问
    JS实现DIV的增加和移动
    让Editplus调试PHP程序
    与UpdatePanel控件不兼容的控件
    在Powerdesigner中,根据已有字段的Name值替换Code相同的Name的值
  • 原文地址:https://www.cnblogs.com/lizhenghao126/p/11053693.html
Copyright © 2011-2022 走看看