[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();
    }

    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

你可能感兴趣的:(Unity)