澄清使用Update()和FixedUpdate()吗? Unity2D [英] Clarifying the use of Update() and FixedUpdate()? Unity2D

查看:518
本文介绍了澄清使用Update()和FixedUpdate()吗? Unity2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我了解到FixedUpdate应该用于物理操作等,但是我在理解什么才是物理方面遇到了麻烦.因此,例如,我通过查看按下了哪些键来移动角色,然后施加力.目前,我正在Update()中执行此操作.该功能是否应该移至FixedUpdate(),因为addForce是要应用于物理对象(播放器,因为它具有刚体组件)的代码?

So I understand that FixedUpdate should be used for physics operations and such but I am having trouble understanding what counts as physics. So for example, I move my character by looking at what keys are pressed then I apply a force. Currently I am doing this in Update(). Should this functionality be moved to FixedUpdate() since addForce is code that's being applied to a physics object (the player, since it has a rigidbody component)?

但是,如果我确实将此代码移至fixedUpdate(),则可以在该方法中运行input.GetKey吗?我读过某个地方,如果在FixedUpdate()中侦听了输入,它们可能会掉线?所以,是的,令人困惑……听起来好像Input.GetKey应该在Update()中,并且当按下一个键时,会激活一个标志.同时,FixedUpdate()中的功能正在监视要激活的适当标志.等等...这种方式是在Update()中处理输入,而移动是在FixedUpdate()中...我能理解这一点吗?

But then, if I do move this code to fixedUpdate(), would it be fine to run the input.GetKey in that method? I read somewhere that inputs can get dropped if they're listened for in FixedUpdate()? So yeah, confusing...It almost sounds like Input.GetKey should be in Update() and when a key is pressed a flag is activated. Meanwhile, the functionality in FixedUpdate() is watching for the appropriate flag to be activated. And such...This way input is handled in Update() and movement is in FixedUpdate()...am I understanding this right?

此外,我正在使用碰撞器和触发器,它们的代码是否也可以在FixedUpdate()中找到?

Also, I am using colliders and triggers, would the code for these also be located in FixedUpdate()?

推荐答案

如果没有良好的资源,这是要理解的最具挑战性的事情之一.这通常是导致大多数初学者错误"的原因.在新的Unity开发人员代码中.

This is one of the most challenging things to understand without good resources. It is often the cause of most "beginner bugs" in new Unity developer's code.

您可能已经阅读过文档,因此可以理解以下内容.

As you may have read in documentation, the following are understood.

初始化顺序:

  1. 醒来
  2. 开始

框架顺序:

  1. 更新
  2. LateUpdate

您可以通过转到编辑>来进一步自定义特定脚本的顺序(例如:始终在PlayerMovement之前运行ScoreManager).项目设置>脚本执行顺序.

You can further customize the order of specific scripts (ex: always run ScoreManager before PlayerMovement) by going to Edit > Project Settings > Script Execution Order.

但是,这不是Unity中正在发生的事情的完整列表.存在物理周期,渲染和输入,它们均按此顺序在不同的时间发生.要注意的关键是有两个脚本执行周期. 框架" (最常见的是-更新),以及固定" (最常见的是-FixedUpdate).

However, this is not a complete list of what's going on in Unity. There are physics cycles, rendering, and input that all occur at different times in this order. The key thing to note is that there are 2 script execution Cycles. "Frame" (most commonly - Update), and a "Fixed" (most commonly - FixedUpdate).

考虑一下脚本执行顺序.

拥有2个脚本执行周期是什么意思?简单地说,所有MonoBehaviour方法都与这2个周期之一相关联.这些方法只执行一次逻辑,直到循环完成.换句话说,您的"PlayerMovement.cs"脚本的Update方法每帧只运行一次.每个固定周期,您的OnCollissionStay只能运行一次.

What does it mean to have 2 script execution cycles? Put simply, all MonoBehaviour methods are tied to one of these 2 cycles. These methods will only perform their logic once until the cycle is complete. In other words, your "PlayerMovement.cs" script's Update method will only run one time for every frame. Your OnCollissionStay will only run one time for every Fixed cycle.

固定循环(FixedUpdate)是:

Fixed Cycles (FixedUpdate) in Unity are:

  • 确定性
  • 基于数学的
  • 不依赖渲染

物理无需渲染精灵或显示UI元素即可计算是否撞到了撞机或是否应施加重力. Unity会分离出特定于物理的数据,以高效地执行计算.此外,很多是确定性的.如果您在一个帧上从(0,0)开始,而刚体在下一帧上移至(10,10)(并且您没有传送),那么我可以合理地假设您在点(5,5)上移动.这使得Unity的物理引擎可以内插"图元.冲突和互动.这意味着物理学不需要看到动作"的每一帧.确定是否发生冲突.固定的周期使Unity可以独立于框架计算物理,并安全地传达这些变化.

Physics doesn't need to have sprites rendered or UI elements visible to be able to calculate if a collider has been hit or if gravity should be applied. Unity separates out the physics-specific data to perform calculations efficiently. Additionally, a lot of this is deterministic. If you started at (0,0) on one frame and your rigibody moved to (10,10) on the next frame (and you did not teleport), I can reasonably assume that you moved through point (5,5). This allows Unity's physics engine to "interpolate" collissions and interactions. This means that physics doesn't need to see every frame of "action" to determine if a collission has occurred. The fixed cycle allows Unity to calculate the physics separately from the frame and communicate those changes safely.

帧周期(更新)是所有Unity交互的基础.没有框架更新游戏就不会发生任何事情.如果不更新框架,那么浪费CPU周期来计算游戏逻辑是没有意义的.这意味着:

Frame Cycles (Update) in Unity are the basis of all Unity's interaction. Nothing can happen without a frame updating the game. There's no point in wasting CPU cycles calculating game logic if the frame isn't being updated. This means that:

  • 输入绑定到框架
  • 帧周期取决于渲染

需要注意的事情:固定周期可能会或可能不会在每帧出现.在较低的帧速率下,每个帧可能会出现多个固定周期.在较高的帧速率下,将发生许多帧而没有任何固定的周期.

Something important to note: fixed cycles may or may not occur every frame. At lower framerates, multiple fixed cycles may occur each frame. At higher framerates, many frames will occur without any fixed cycles.

在大多数情况下,这两个周期可以很好地协调工作.引起关注的最常见原因如下:

For the most part, these 2 cycles work in harmony fairly well. The most common cause for concerns are the following:

  • 尝试使用变形" 物理学(即刚体)更新GameObjects位置.您应该尝试仅在两个周期之一中更新GameObjects位置.如果希望GameObject与物体碰撞或与重力等物理相互作用,请使用Rigidbody.move或Rigidbody.velocity.

  • Trying to update a GameObjects position using Transform and physics (i.e. Rigidbody). You should try to only update a GameObjects position on one of the 2 cycles. If the GameObject is expected to collide with things or interact with physics like gravity, use Rigidbody.move or Rigidbody.velocity.

尝试以固定周期读取输入.由于固定周期每帧可能发生0次很多,因此可能会错过输入.如果在一帧上使用GetButtonDown(),但是FixedCycle不运行,则将错过"消息.在FixedUpdate中输入的内容.通常最好使用布尔值或队列来管理输入与物理系统之间的差异. (就像在Update中设置bool jump = true,并在实际执行物理组件时在FixedUpdate中读取/更新bool jump)

Trying to read input in the Fixed cycle. Since the fixed cycle can occur 0-many times per frame, input may be missed. If you use GetButtonDown() on one frame but the FixedCycle doesn't run, you will "miss" that input in FixedUpdate. It's usually best to use bools or queues to manage this discrepancy between input and the physics system. (Like setting a bool jump = true in Update, and reading/updating the bool jump in FixedUpdate when you actually perform the physics component)

这篇关于澄清使用Update()和FixedUpdate()吗? Unity2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆