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

Question Time.deltaTime returning different values under certain conditions

Discussion in 'Editor & General Support' started by theguyuknow, Jul 19, 2023.

  1. theguyuknow

    theguyuknow

    Joined:
    Jan 24, 2015
    Posts:
    3
    Hey all,

    First time poster. If I'm breaking any rules, please let me know.

    Background
    I have three scripts that need to coordinate to move the player, the camera, and the backgrounds in sync. I have accomplished this by having one script register an action when the necessary conditions are met, and the other two listen and invoke their needed functions when this action happens.

    Issue
    The two listener scripts are out of sync on the initial load of the level. This results in the camera and the backgrounds being on slightly different timings. However, if the scene is reloaded, the timing issues go away and everything is in-sync. The issue never returns until the game is exited and fully loaded again.

    I assumed I was initializing these objects in some way that made them out-of-sync on initial loads but in-sync on scene restarts. However, they are using the same variables and initialized to the same values.

    I've isolated the issue down to one symptom: in Update(), Time.deltaTime is returning different values each frame for these two objects. Again, this is only true on the initial level load. Once the scene is restarted, the sync issues go away and Time.deltaTime matches between the two objects.

    Question
    Does anyone have any insight into how this could be happening?

    Please let me know if more technical details are necessary.

    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    ALL calls to Time.deltaTime in a given frame should return the same value.

    If it doesn't, and if you created a game design with such a tight timing requirement, make a timer manager and have everybody respect that instead.

    For instance, it might return a custom ever-incrementing time, plus a deltaTime that you know is consistent for everybody in that frame.

    Otherwise, it should be fairly easy to tolerate irregular frame timing and I highly recommend doing so.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    It's actually up to YOU to verify this first, and you ARE violating rules by spamming two posts.

    Now I have wasted time writing back to this post when others have already written to the first one.
     
  4. theguyuknow

    theguyuknow

    Joined:
    Jan 24, 2015
    Posts:
    3
    I tried to delete this thread once I realized it may be in the wrong forum, but the options for thread management aren't clear. I couldn't (and still can't) find how to remove my own post. I understand it is frustrating having people dual post, but if users could easily manage their own mistakes, less of it would fall on mods.

    Thanks for your comment about deltaTime. It isn't returning the same, even if it should be.
     
    Last edited: Jul 19, 2023
  5. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    702
    Usually you move your camera in lateupdate.

    Update is haphazard, so your update calls maly be waiting for the next frame to do their thing. For example, if update calls the camera before the character, it reads the character's last known position, given in previous frame, then adjusts, then the character updates its position, but camera has already done its update, and using character's position from previous frame.

    Use Update to change data, LateUpdate to act on changed data.
     
  6. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    702

    Your other thread is already gone. Dont sweat it.

    Kurt's in a bad mood this decade.
     
    theguyuknow likes this.
  7. theguyuknow

    theguyuknow

    Joined:
    Jan 24, 2015
    Posts:
    3
    Thank you. A little kindness goes a long way.

    Also, I am getting closer to a solution. Both listening functions are activated by the same event; however, the first time the scene loads, these events always get activated a frame apart. Function A is activated on frame 1 and function B is activated on frame 2.

    Again, this is only true for the first time the scene loads. For any subsequent loads of the scene, the event registered functions start on the same frame.

    I'll have to research why this may be.

    Edit: I'll also look into LateUpdate as well. I appreciate the suggestion.