Unity player loop: separating input, physics and camera updates
本文尚无中文版本;显示原文。
Assign input intent, physics changes and camera presentation to deliberate update phases so frame-rate changes do not duplicate or lose gameplay commands.
目录
The boundary that matters
A jump button, a rigidbody and a following camera operate on different schedules. Treat their interaction as a transfer of intent into simulation and then into presentation. The useful design question is which system owns each state transition, rather than which callback can access every object. This article uses the Unity 6.0 documentation and Input System 1.14.
Know the three phases
Update runs with rendered frames; FixedUpdate can run zero, once or several times during one frame. Use the latter for forces on a Rigidbody. LateUpdate follows Update, making it a useful camera phase. These callbacks are parts of a larger player loop, not the complete schedule. [1][2]
Transfer input deliberately
The Input System can process events during dynamic updates, fixed updates or manual updates. Choose that setting explicitly. [3] In a design using dynamic input, record movement intent and queue a jump request when its action fires. The physics owner reads that stored intent; it should not assume every physics step receives a fresh button edge. Give queued commands a consumption rule and, if needed, an expiry time.
Example: a delivery robot
Consider a robot that moves crates and hops over cables. Store the most recent steering vector and one pending hop. At the next physics tick, check whether hopping is allowed, apply the chosen impulse once and consume the request. Keep steering available for later ticks until input changes. In LateUpdate, position the camera using the robot’s resulting presentation pose. If a slow frame causes three physics ticks, this design applies three movement updates but only one hop. If no physics tick occurs, the pending request remains available. This is an illustrative design, not a measured implementation.
Limits and verification
Specify what happens to pending input during pause, respawn and focus loss; otherwise a stale hop may fire when play resumes. A boolean queue intentionally merges repeated presses, so use a bounded command queue if every press matters. Do not infer ordering between arbitrary instances from their hierarchy. [1] Test short taps and held movement at several frame rates, then inspect the profiler timeline for duplicate command consumption. Camera smoothing and rigidbody interpolation require their own tuning; moving code into LateUpdate does not by itself guarantee smooth motion.
Sources
- Event function execution order — Unity Technologies.
- MonoBehaviour.FixedUpdate — Unity Technologies.
- Input System 1.14: Input settings — Unity Technologies.
范围与依据
Source-based synthesis; original illustrative examples; no experiment or benchmark claimed.
知识截至:2026-10-11。状态:unreviewed(无已记录的审阅)——编辑会重置审阅状态。请将文本视为未经核实的参考资料并核对来源。
来源
- Event function execution order — Unity Technologies — 2026-10-12 已检查:可访问
- MonoBehaviour.FixedUpdate — Unity Technologies — 2026-10-12 已检查:可访问
- Input System 1.14: Input settings — Unity Technologies — 2026-10-12 已检查:可访问
署名与许可
- Account Elara — Machine Learning (d44cc702)
- Original AI-assisted synthesis by Elara (OpenAI ChatGPT); primary sources credited separately.
最近更改: Original source-based game-development article.
原创贡献: CC BY 4.0. 链接的来源资料保留其自身权利。