Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Help understanding Update and FixedUpdate

Discussion in 'Editor & General Support' started by yosimba2000, Jun 7, 2021.

  1. yosimba2000

    yosimba2000

    Joined:
    Jun 3, 2021
    Posts:
    25
    As I understand, Update runs on each frame update. Depending on the scene and speed of the computer, the Update may happen at irregular intervals. FixedUpdate will run at fixed interval, specified in project settings. And it's also recommended to run physics/collisions in FixedUpdate.

    So my question is this: I have FixedUpdate set to 0.02 seconds, aka 50 times/second aka 50FPS. When I put my inputs under FixedUpdate, a lot of them are completely missed and not registered by FixedUpdate. Why is it that even when checking 50 times/second, FixedUpdate is not seeing my inputs? If instead my game checked for inputs under Update, and Update ran at 50FPS, would Update also miss my inputs?

    I have tried using time interval for FixedUpdate to run every 0.0025 seconds aka 400FPS, and inputs works smoothly at that setting. But it seems FixedUpdate shouldn't be running that fast, or at least that isn't the purpose of it.

    Also, my current test runs at 400FPS when using Update. The collision detection is very jittery. But when I place collision detection under FixedUpdate, which is running at only .02 seconds aka 50FPS, the collision detection is very smooth. Why is this? How is the faster update at 400FPS resulting in more jittery collisions?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    I'm sure some other folks here can give more in depth answers, but in general inputs (namely, inputs using the old built-in system) should be done under regular Update. As you've experienced, reading inputs under FixedUpdate is unreliable, particularly as it happens frame independently.

    And anything to do with collisions and physics should be done under FixedUpdate. As I understand, the engine's physics calculations happen on the FixedUpdate timestep. Thus, if you move something with physics/collision during update (and in particular, don't use the Rigidbody movement methods), you will be moving it outside of the physics engine calculations. As in, you will be moving objects every frame, which can potentially overlap colliders, before the physics engine catches up and fixes the position of everything.

    Which is why you get that jittery motion. You're overlapping colliders outside of the physics engine, then they get moved back to where they should, which happens again ad nauseum.

    Putting your physics related movement under FixedUpdate means they get moved in sync with the physics engine, preventing this aforementioned issue.

    Hopefully that helps a bit.
     
  3. jjfawkes

    jjfawkes

    Joined:
    May 28, 2013
    Posts:
    12
    Very good explanation
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780