Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why game engines like Unity or CryEngine uses different frame updates.

Discussion in 'Scripting' started by Most_Incredible, Aug 14, 2014.

  1. Most_Incredible

    Most_Incredible

    Joined:
    Jan 25, 2012
    Posts:
    36
    Hi ,
    I have encountered in more than one game engine using diffrent frame updates , for example
    in unity they use Update for game logic,FixedUpdate for Physics, OnGUI for UI.Like wise in CryEngine
    Update() and PostUpdate.Is there a specific reason to have diffrent Update functions.
    I guess it is to make code Modular.

    Thanks
     
  2. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Check out the Execution Order page in the manual - the explanations of the events give a good idea as to why the separation exists.
     
  3. Most_Incredible

    Most_Incredible

    Joined:
    Jan 25, 2012
    Posts:
    36
    NomadKing,
    Thanks for the fast replay ,I looked at the link.It is not answering me why it is there in the first place.It is just answering me what it is.
     
  4. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Sorry, I thought the explanations (and the diagram at the bottom) kind of implied the answer. Basically they exist because they all fire at different times, which is helpful when dealing with the different systems of a game engine as well as specific use cases.

    Examples:
    Doing movement in the FixedUpdate rather than Update because of when physics updates are done.
    Doing GUI in OnGUI because it's called after the rest of the scene has been rendered and GUI is typically draw over the top.
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    They are for different purposes and as such it makes a lot of sense to split them up.
    Physics is computed in fixed time intervals to improve the physical stability and as such the believability. As a consequence, it can be executed more than once per frame. That is the reason why FixedUpdate exists in Unity. Update is called once per frame, just like LateUpdate.

    Edit: NomadKing was faster :)
     
    Most_Incredible and NomadKing like this.
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    It's there in the first place because a game engine has several tasks to perform.

    Physics Calculations
    Rendering
    Input Handling
    Rendering GUI

    These are different tasks, and they all get done in order because it just is easy to do them all in order. For instance the GUI is rendered after the scene graphics. This is because a GUI is ON TOP of the scene. If you rendered it before, it'd be behind. And if it was rendered at the same time random objects would be in front of the gui, and others behind it, depending on which was done first.

    So basically all these jobs are divvied up and done in an order that makes sense.

    THEN because it's in that order. They make available various events (the Update method, the FixedUpdate method, etc) that occur in between those tasks allowing you to hook in before or after various jobs.

    Some of those jobs will also prep up the state of the engine so that you can also access various things. So you want to render to the GUI... ok, well while the engine is rendering the GUI, it preps up everything for rendering, then calles the OnGUI event, and you can access it. If you accessed drawing the GUI at any other time the engine wouldn't be prepared and you'd be drawing to nothing.
     
    Most_Incredible and NomadKing like this.
  7. Most_Incredible

    Most_Incredible

    Joined:
    Jan 25, 2012
    Posts:
    36
    Thanks for the convincing answer.
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Last edited: Sep 9, 2014
    Most_Incredible likes this.