Unity核心原理(2)深入理解FixedUpdate

Unity核心原理(2)深入理解FixedUpdate

官方文档链接:Time and Framerate Management

Fixed Timestep
Unlike the main frame update, Unity’s physics system does work to a fixed timestep, which is important for the accuracy and consistency of the simulation. At the start of the physics update, Unity sets an “alarm” by adding the fixed timestep value onto the time when the last physics update ended. The physics system will then perform calculations until the alarm goes off.
和Update主帧循环不同,Unity的刚体系统通过固定的时间来驱动,固定的运算时间是保证模拟结果准确一致的重要因素 。在刚体系统运算开始前,Unity会根据上一次刚体运算完成的时间再加上Fixed Timestep的所设置的值来作为这一次刚体运算时间的限定范围。然后刚体系统会开始计算,直到达到这个时间限制临界点。

You can change the size of the fixed timestep from the Time Manager and you can read it from a script using the Time.fixedDeltaTime property. Note that a lower value for the timestep will result in more frequent physics updates and more precise simulation but at the cost of greater CPU load. You probably won’t need to change the default fixed timestep unless you are placing high demands on the physics engine.
通过Time Manager可以调整Fixed Timestep的值,也可以在脚本中通过访问Time.fixedDeltaTime属性来读取这个值。注意,更低的Fixed Timestep值会让刚体模拟次数更频繁,结果也更精确,但代价是CPU的性能开销也会更大。除非你对物体引擎有更高的要求,否则一般不建议更改默认的设置。
Maximum Allowed Timestep
The fixed timestep keeps the physical simulation accurate in real time but it can cause problems in cases where the game makes heavy use of physics and the gameplay framerate has also become low (due to a large number of objects in play, say). The main frame update processing has to be “squeezed” in between the regular physics updates and if there is a lot of processing to do then several physics updates can take place during a single frame. Since the frame time, positions of objects and other properties are frozen at the start of the frame, the graphics can get out of sync with the more frequently updated physics.
Fixed Timestep保证了刚体模拟的实时准确运算,但也会导致一些问题,当游戏中物理运算量比较多时,会导致游戏帧率变低(因为游戏中的物体数量非常多,运算量会非常大)。而游戏的主循环(Update)必须在在常规的物理运算FixedUpdate()调用之间进行,当有大量的物体运算要进行处理时就会在一个主循环帧(Update)当中进行多次物理运算(FixedUpdate).在主循环帧开始的时候,物体的位置和其它属性都是固定不变的,因此在这一帧里显卡所显示的最终结果与更高频率的物理运算结果是不同步的。(Plane备注:其实就是浪费了计算资源,因为中间的计算的结果是不会显示的。)

Naturally, there is only so much CPU power available but Unity has an option to let you effectively slow down physics time to let the frame processing catch up. The Maximum Allowed Timestep setting (in the Time Manager) puts a limit on the amount of time Unity will spend processing physics and FixedUpdate calls during a given frame update. If a frame update takes longer than Maximum Allowed Timestep to process, the physics engine will “stop time” and let the frame processing catch up. Once the frame update has finished, the physics will resume as though no time has passed since it was stopped. The result of this is that rigidbodies will not move perfectly in real time as they usually do but will be slowed slightly. However, the physics “clock” will still track them as though they were moving normally. The slowing of physics time is usually not noticeable and is an acceptable trade-off against gameplay performance.
当然,CPU的资源是有限的,Unity有一个设置项,可以有效地降低物理运算时间,以便于让主循环可以和追上物理运算的帧率。Time Manager中的Maximum Allowed Timestep就是给处理物体运算的FixedUpdate加上一个限制。如果某一帧的物理运算时间超过了这个值,物理引擎就会立即停止运算,以便让主循环Update可以追上,一旦这一帧主循环Update运行完成,物理引擎就会从它暂停的地方恢复计算就像它从来没有停止过一样。这样做的结果是,刚体不会像平时那样实时完美地移动,而是会稍微放慢速度。 然而,物理运算结果的连贯性得到了保证,就像它们在正常移动一样。通常情况下,物理运算结果变慢的程度不太明显,这是一个可接受的性能与表现之间的平衡。

注意:本内容来自:https://zhuanlan.zhihu.com/p/55336653 

你可能感兴趣的:(Unity基础)