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

How to make WaitForFixedUpdate() TimeScale independent?

Discussion in 'Scripting' started by TimNedvyga, Sep 20, 2018.

  1. TimNedvyga

    TimNedvyga

    Joined:
    May 18, 2015
    Posts:
    95
    Hi guys,

    I'm trying to do custom TimeScale independent FixedUpdate function using coroutine. Its looks like this at the moment:

    Code (CSharp):
    1. private IEnumerator CustomFixedUpdate()
    2.     {
    3.         while (true)
    4.         {
    5.            //Do Something
    6.            
    7.             yield return new WaitForFixedUpdate();
    8.         }
    9.     }
    But problem is that WaitForFixedUpdate(); is TimeScale dependent. I need something like
    WaitForUnscaledFixedUpdate();
    Maybe someone knows how to do it manually?
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I really don't see how this would be any different than:
    Code (csharp):
    1.  
    2. private void FixedUpdate()
    3. {
    4.     //Do Something
    5. }
    6.  
    But, that's besides the issue. I myself prefer to just use Threads instead of Coroutines, just because I'm used to that. The main disadvantage is that you are limited in what you can do, since this runs besides the main thread. It does allow you to choose your own running interval, using Thread.Sleep. So, it all depends on "Do Something" whether this is a solution or not.

    Another side note, Threads and Coroutines don't automatically stop when you stop the main thread. So instead of while(true) you might want something like while(Application.isPlaying).
     
  3. TimNedvyga

    TimNedvyga

    Joined:
    May 18, 2015
    Posts:
    95
    FixedUpdate() is not called when timeScale = 0.
    I want to recreate FixedUpdate with coroutine but timeScale independent.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What are you trying todo with that?
     
  5. Omid7L

    Omid7L

    Joined:
    Jun 10, 2018
    Posts:
    14
    yield return new WaitForEndOfFrame() working while timescale is zero;)