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

Execute a function in LateUpdate in a fixed time

Discussion in 'Scripting' started by eidercarlos, Jul 9, 2015.

  1. eidercarlos

    eidercarlos

    Joined:
    May 14, 2014
    Posts:
    9
    Hi Guys!
    How to call a function in LateUpdate() in a fixed (constant) time like in FixedUpdate() in Unity3d C#?
    Is this possible?
    Thanks!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    you want a LateFixedUpdate?

    Basically a FixedUpdate that occurs after all other FixedUpdates have completed?

    What you can do is use script execution order to make your script be the last script that gets called. It will effectively be the last one called.

    http://docs.unity3d.com/Manual/class-ScriptExecution.html
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    If you need a mix bag, where the same script does both regular FixedUpdate AND LateFixedUpdate... well then you're going to have to break out the LateFixedUpdate to another script.

    Why I did was create a UpdateEventHooks script:
    https://github.com/lordofduct/space...ster/SpacepuppyBase/Hooks/UpdateEventHooks.cs

    This creates a generalized event structure for easily hooking into another scripts update events. Then I created an 'early' and a 'tardy' version of the scripts that inherit from this UpdateEventHooks:

    https://github.com/lordofduct/space...yBase/Hooks/TardyExecutionUpdateEventHooks.cs

    And set their respective execution orders to be the first and last:
    https://github.com/lordofduct/space.../Hooks/TardyExecutionUpdateEventHooks.cs.meta

    Now in your script you can be like:

    Code (csharp):
    1.  
    2. public class MyScript : MonoBehaviour
    3. {
    4.  
    5.     private TardyExecutionUpdateEventHooks _tardyHooks;
    6.    
    7.     void Awake()
    8.     {
    9.         _tardyHooks = this.AddComponent<TardyExecutionUpdateEventHooks>();
    10.         _tardyHooks.FixedUpdateHook += this.LateFixedUpdate;
    11.     }
    12.    
    13.    
    14.     void FixedUpdate()
    15.     {
    16.         //do regular update
    17.     }
    18.    
    19.     void LateFixedUpdate(object sender, System.EventArgs e)
    20.     {
    21.         //do late update
    22.     }
    23.    
    24. }
    25.  
     
    eidercarlos likes this.
  4. eidercarlos

    eidercarlos

    Joined:
    May 14, 2014
    Posts:
    9
    Thank you Very Much lordofduct !!
    I Will try to do this!! My desire is basically the FixedUpdate after the Animations execution (LateFixedUpdate)
    Because normally is used the LateUpdate function to do things after the animation! but there is no function like this implemented to do it in a fixed time interval (sorry for my english).
    Anyway...Thanks!