Unity中的各类Update函数

本文深入探讨Unity中的Update、FixedUpdate和LateUpdate函数,解释它们的执行时机和应用场景。物理运算应放于FixedUpdate,Input检测应在Update中处理,而Camera跟踪适合LateUpdate,以确保正确跟踪已移动的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

我们可能经常会在一些资料中看到何时应该运用何种Upate函数,例如:

  1. 物理运算应该放在FixedUpdate中
  2. Input检测应该放在Update中
  3. 相机应该放在LateUpate中

但是却不知道所以然。本文将会以几个案例来分析上面的新手指令清单的更深一层的原因。

定义

首先,我们先从官网简单看下不同的定义,后面再通过案例详细说明。

Update

Update is called every frame, if the MonoBehaviour is enabled.

Update is the most commonly used function to implement any kind of game script. Not every MonoBehaviour script needs Update.

上面重点说的是Update每帧都会被调用。

FixedUpdate

Frame-rate independent MonoBehaviour.FixedUpdate message for physics calculations.

首先FixedUpdate和帧率是不相关的。

MonoBehaviour.FixedUpdate has the frequency of the physics system; it is called every fixed frame-rate frame. Compute Physics system calculations after FixedUpdate. 0.02 seconds (50 calls per second) is the default time between calls. Use Time.fixedDeltaTime to access this value. Alter it by setting it to your preferred value within a script, or, navigate to Edit > Settings > Time > Fixed Timestep and set it there. The FixedUpdate frequency is more or less than Update. If the application runs at 25 frames per second (fps), Unity calls it approximately

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值