[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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值