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. Dismiss Notice

Basic question re: fixed update

Discussion in 'Scripting' started by kidmethuselah, Apr 7, 2021.

  1. kidmethuselah

    kidmethuselah

    Joined:
    Jan 14, 2021
    Posts:
    16
    Hi,

    Is there an equivalent to FixedUpdate that works when time.timescale = 0?

    I have a function that currently executes in Update, which means it sometimes goes quicker/slower depending on framerate. I need it to happen at a fixed rate. The obvious solution would be to execute the function in FixedUpdate, but at the time the function occurs, time.timescale is set to 0.

    (I have tried googling this but I can only find threads where people are asking the opposite question (how can I make things stop happening when time.timescale = 0))

    Thanks!
     
  2. timfrombriz

    timfrombriz

    Joined:
    Jun 23, 2014
    Posts:
    30
    FixedUpdate is not what you should be using. If its a fixed real world clock you need to execute against then...

    Either update(), with a float time Elapsed += time.deltatime and check when your time elapsed exceeds the desired execution rate, or a coroutine with yield waitforseconds.
     
  3. kidmethuselah

    kidmethuselah

    Joined:
    Jan 14, 2021
    Posts:
    16
    Thanks for your response.

    This is my current code:

    Code (CSharp):
    1.  void Update()
    2.     {
    3.  
    4.         if (finished == true)
    5.         {
    6.  
    7.             scoreAppearsTimer += Time.fixedDeltaTime;
    8.  
    9.             if (scoreAppearsTimer > 0.5f && scoreAppearsTimer < 0.51f)
    10.             {
    11.                 totalTimerGO.gameObject.SetActive(true);
    12.             }
    13.  
    14.             if (scoreAppearsTimer > 0.75f)
    15.             {
    16.                 plus1.gameObject.SetActive(true);
    17.             }
    18.  
    19.             if (scoreAppearsTimer > 1)
    20.             {
    21.                 totalDiceGO.gameObject.SetActive(true);
    22.             }
    23.  
    24.             if (scoreAppearsTimer > 1.25f)
    25.             {
    26.                 plus2.gameObject.SetActive(true);
    27.             }
    28.  
    29.             if (scoreAppearsTimer > 1.5f)
    30.             {
    31.                 totalDeathGO.gameObject.SetActive(true);
    32.             }
    33.  
    34.             if (scoreAppearsTimer > 2.5f)
    35.             {              
    36.                 finalScoreGO.gameObject.SetActive(true);
    37.             }
    38.  
    39.             if (scoreAppearsTimer > 3f)
    40.             {
    41.                 scoreAppearsTimer = 3;
    42.             }
    43.  
    44.         }
    45.  
    46.     }
    To clarify, should that be working at a constant rate (against a real world clock, as you state)? If so, then I guess my problem lies elsewhere (as I'm trying to get multiple things to sync up on different scripts against a 3 second timer)

    Thanks again for answering
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Code (CSharp):
    1. scoreAppearsTimer += Time.fixedDeltaTime;
    is not correct. It should be:
    Code (CSharp):
    1. scoreAppearsTimer += Time.deltaTime;
     
  5. kidmethuselah

    kidmethuselah

    Joined:
    Jan 14, 2021
    Posts:
    16
    Thanks Praetor Blue, but that is the core issue.

    As per the original question, time.timescale is set to zero when this function occurs, so Time.deltaTime is not moving the timer. Time.fixedDeltaTime doesn't seem to be syncing with actual seconds either. Should it be?
     
  6. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    I believe you're looking for Time.unscaledTime
     
    kidmethuselah likes this.
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Sorry,
    Code (CSharp):
    1. scoreAppearsTimer += Time.unscaledDeltaTime;
     
  8. timfrombriz

    timfrombriz

    Joined:
    Jun 23, 2014
    Posts:
    30
    Code (CSharp):
    1.  
    2.             if (scoreAppearsTimer > 0.5f && scoreAppearsTimer < 0.51f)
    3.             {
    There is no guarantee Update() will execute during this period.

    It looks like your animating a process, I would instead increment a frame counter at a fixed time as you are doing and record the last executed frame , and execute from that frame to current expected frame based on elapsed time. That ensures any latency in the process will not result in parts being skipped.
     
    PraetorBlue likes this.
  9. kidmethuselah

    kidmethuselah

    Joined:
    Jan 14, 2021
    Posts:
    16
    This bit of code was seriously dumb. Noticed that last night and changed it.

    As for the main question, Time.unscaledTime worked great and I thank you all for all your help!
     
    Kurt-Dekker likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Also since this thread is a lot to do with timing and whatnot, let me offer you this awesome graphic from the Unity docs, which can help you stick it all into the proper places in your brain:

    https://docs.unity3d.com/Manual/ExecutionOrder.html