Search Unity

Update vs Fixed Update vs Late Update

Discussion in 'Physics' started by ChrisX, Mar 5, 2015.

  1. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Hi all.

    If this isn't in the proper forum, please move it to the proper one. I'm still new here...

    Anyway, I'd like to know about the fundamentals behind Update, Fixed Update and Late Update. The definitions, I get already, Update is executed every frame, Fixed Update is executed in a fixed time per frame, Late Update is executed after the Update circle is done. My questions are

    1. When should you use Update and when should you use Fixed Update and when should you use Late Update? And... what happens when you misplace codes (e.g: Put what should be in Fixed Update in Update instead, put what should be in Update in Fixed Update instead)
    2. How do we know the fixed time used in Fixed Update, and can we manipulate it?
    3. How important is Late Update? Why do we need it when we can put it within Update?

    Thanks!
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    1. a
    Update: Most things
    Fixed Update: Physics things
    LateUpdate: Things that need to happen after update/right before the camera renders

    1. b
    If you added a force every Update instead of FixedUpdate, your game would run differently depending on the frame-rate.

    2. Edit -> Project Settings -> Time
    From scripting, check out the Time class.

    3. A common example is for a camera controller that should be centered on a moving object. If both of them are moving in Update there is a chance that the camera may move first, then the object will move making it no longer centered. Moving the camera in LateUpdate makes sure that the object is moved first, then the camera centers on it.
     
  3. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    1. b. And what happens if you do the reverse, that is, putting what should be in Update in Fixed Update instead?
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    If you put movement code such as translate into FixedUpdate rather than Update the movement will appear jagged.
     
    Tonymotion likes this.
  5. azurvii

    azurvii

    Joined:
    Mar 26, 2015
    Posts:
    5
    @DanielQuick Thanks. Re:3.: What is the difference between using LateUpdate() and put the camera movement code at the bottom of Update()?
    I think the rendering does not start after the whole Update() finishes, right?
     
  6. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    What if you have something that is moving in Update, but is not in the same script as the one moving the camera?
    Having the camera movement in the bottom of the update loop doesn't mean it will run at the very end, after all the other stuff stopped moving as different scripts may run in different times. There is a limit to a computer's resources and so scripts are not executed all at the same time. (There is even a place in unity where you can force a whole script to run after a certain other script)
    LateUpdate will ensure that what is in that loop will run after every single other Update loop in all the other scripts.
     
  7. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
    This sounds like a good candidate for a new page in the scripting section of the manual. I'll see what I can do.
     
  8. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    @duck and please add a note in the documentation regarding JS and SceneManagement that you need to add
    import UnityEngine.SceneManagement at the start of said scripts.
     
  9. azurvii

    azurvii

    Joined:
    Mar 26, 2015
    Posts:
    5
    Thanks @screenname_taken, I was also reading http://docs.unity3d.com/Manual/ExecutionOrder.html, and got a little bit more confused over the order. From the flowchart at the bottom of the article, the "Screen rendering" stage takes place after LateUpdate, which is after Update.
    So does it mean that the camera kicks off rendering when it's moved, and does not follow the illustrated order? or does it take a snapshot of its view and then do the rendering after LateUpdate?
    My confusion here is my unclear understanding of the order of events: I assume the camera "takes a shot" only in the rendering stage, but it seems I'm wrong?
     
  10. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    It depends when you move the camera. If you move it in LateUpdate or Update, then it'll move and then render.
    The order of execution is the one in the flowchart.
    Rendering to the screen happens after LateUpdate.
     
  11. ookk47oo

    ookk47oo

    Joined:
    Mar 17, 2017
    Posts:
    80
    So does this mean camera render may happen between update functions?
     
  12. DWriedt

    DWriedt

    Joined:
    Oct 7, 2019
    Posts:
    2
    I'm still new, but the way that I understand it is this. You want to do your physics checks in FixedUpdate, because if you did it in Update, one frame later you may be past a collision. Fixed update might repeat 1-20 times in the time one Update runs. You don't want to be past a collision and it not reporting there is a collision, because it's overshot it's target.
     
    tomachinz likes this.
  13. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Update and FixedUpdate may be seen as two separate execution paths, not necessarily "after" or "before" each other.

    Update
    • Executes on each visually presented frame. When you see 134 FPS in the stats it means 134 Update cycles per second.
    • Time.deltaTime is the time between one update cycle to another.
      Updated for Unity 2020.2: If VSync is enabled then Update is called at the display's refresh rate and Time.deltaTime is the time between each frame presentation (typically 1 / refresh rate).
    • Frames may be skipped sometimes if the cpu/gpu load can't keep the display rate.
    FixedUpdate
    • Executes at a fixed rate (50 Hz by default, defined in Project Settings > Time > Fixed Timestep).
    • Time.deltaTime is always the value specified in the Fixed Timestep setting.
    • FixedUpdate cycles won't be skipped ever: instead, the game time may be "slowed down" and/or Update calls will be skipped in order to perform every single FixedUpdate call.
    There are situations where multiple FixedUpdate calls are performed between each Update call. Most frequently, Update is called several times between each FixedUpdate call.

    Rule of thumb:
    • Update: visual stuff, camera, effects, things that may be adapted to varying delta time, and skipped for saving CPU/GPU.
    • FixedUpdate: physics, gameplay, AI, things that depend on precise timing and/or would affect gameplay if skipped.
    Putting everything in FixedUpdate is incorrect. The game should be able to skip things to adapt to situations of intensive CPU/GPU usage. Otherwise, gameplay will just slow down in those cases.

    LateUpdate executes in the same cycle as Update, but after all Update functions have been called.
     
    Last edited: Jul 6, 2022