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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Time.deltaTime Not Constant: VSync CameraFollow and Jitter

Discussion in 'General Graphics' started by Zullar, Sep 9, 2016.

  1. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    You'll be able to insert code before "PumpOSMessages", effectively pushing PumpOSMessages later in the frame.

    It opens Feedback Hub on my machine. Does it not on yours?
     
    Ultroman and SonicBloomEric like this.
  2. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,048
    @AnsisMalinsUbisoft wants to get the most up-to-date input messages they can. How best to do this? Do all input-agnostic processing before running the PumpOSMessages logic. This would allow them to process input that the system detected while their input-agnostic logic was processing.

    They simply wish to do Input-dependent logic as close to the Input "reading" as possible.

    No. The link leads to a Microsoft Feedback Hub link. It requires the Feedback Hub app to open the relevant information.
     
  3. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    206
    Oh I see, it was blank page. I'm not gonna install some MS app to see it tho, thanks.

    Hmm, I see. Not sure it would be much of a gain, FPS have to be pretty high for it to really matter imho. But then higher FPS implies that logic processed very fast as well so...
     
  4. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,048
    Actually, the lower the FPS the more this would matter. If you are able to hit a frametime of 3ms then by definition your response time is capped at 3ms.

    If your frametime, however, is 45ms-or-more (lower framerate) then it can start to feel unresponsive.
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    Last edited: Oct 20, 2020
    SonicBloomEric likes this.
  6. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,048
    Is there a way to share the information with those that are not on Windows 10?
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    No idea :(. But Microsoft asks us to report all OS bugs through there.
     
    Ultroman and SonicBloomEric like this.
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    I have a better vision of this now. You could do something like this:

    Code (csharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Runtime.InteropServices;
    4. using System.Threading;
    5. using UnityEngine;
    6. using UnityEngine.LowLevel;
    7. using UnityEngine.PlayerLoop;
    8. using UnityEngine.Profiling;
    9.  
    10. public class NewBehaviourScript : MonoBehaviour
    11. {
    12.     struct CustomTimeUpdate
    13.     {
    14.         public static PlayerLoopSystem.UpdateFunction s_EngineTimeUpdate;
    15.  
    16.         public static void OnTimeUpdate()
    17.         {
    18.             Profiler.BeginSample("Before WaitForLastPresentationAndGetTimestamp");
    19.             Profiler.EndSample();
    20.  
    21.             s_EngineTimeUpdate?.Invoke();
    22.  
    23.             Profiler.BeginSample("After WaitForLastPresentationAndGetTimestamp");
    24.             Thread.Sleep(10);
    25.             Profiler.EndSample();
    26.         }
    27.     }
    28.  
    29.     void Start()
    30.     {
    31.         var playerLoop = PlayerLoop.GetDefaultPlayerLoop();
    32.         WalkPlayerLoop(ref playerLoop, 0);
    33.         PlayerLoop.SetPlayerLoop(playerLoop);
    34.     }
    35.  
    36.     private void WalkPlayerLoop(ref PlayerLoopSystem currentSystem, int depth)
    37.     {
    38.         if (currentSystem.type == typeof(Initialization.PlayerUpdateTime))
    39.         {
    40.             var functionPtr = Marshal.ReadIntPtr(currentSystem.updateFunction);
    41.             if (functionPtr != IntPtr.Zero)
    42.                 CustomTimeUpdate.s_EngineTimeUpdate = Marshal.GetDelegateForFunctionPointer<PlayerLoopSystem.UpdateFunction>(functionPtr);
    43.  
    44.             currentSystem.type = typeof(CustomTimeUpdate);
    45.             currentSystem.updateFunction = IntPtr.Zero;
    46.             currentSystem.updateDelegate = CustomTimeUpdate.OnTimeUpdate;
    47.         }
    48.  
    49.         if (currentSystem.subSystemList != null)
    50.         {
    51.             int subSystemCount = currentSystem.subSystemList.Length;
    52.             for (int i = 0; i < subSystemCount; i++)
    53.             {
    54.                 WalkPlayerLoop(ref currentSystem.subSystemList[i], depth + 1);
    55.             }
    56.         }
    57.     }
    58. }
    This callback (CustomTimeUpdate.OnTimeUpdate) would get called in place of "WaitForLastPresentationAndGetTimestamp()". Then, you can do stuff before it, forward the call to the original engine function and then do stuff afterwards too. This code will compile in current Unity versions too, but the behaviour will be slightly different:

    • In 2020.2 (after my fix), this will get called before OS events are pumped.
    • In the current 2020.2 beta, this will get called after OS events are pumped, but the original engine function will be null: the time was updated before OS events had been pumped (this is due to the issue I introduced).
    • In 2020.1 and older, this will get called after OS events are pumped.

    Also, it's not going to be called "Initialization.PlayerUpdateTime" after my fix lands. I had to move it to its own player loop category. However, this code will continue working because script updater will handle the automatic rename.
     
    Last edited: Oct 20, 2020
    SonicBloomEric, rz_0lento and valarus like this.
  9. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,357
    Just to be clear, new input system relies on this snapshot still and doesn't do it's own polling? I ask because I was under impression that the new input system could do it's own polling outside of the main loop.

    If the new input system relies on only getting new data at a specific moment in the player loop, it's totally pointless that it lets even users select where it fires in the player loop (there's a project wide setting for this on the input system).

    I trigger it manually because I have custom devices that I want to poll at the exact moment I trigger the poll and that definitely works without having to wait for another main loop iteration but it also uses my own native plugin so it's not tied to Unity internals..
     
    Last edited: Oct 21, 2020
  10. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Depends largely on the device and platform. There's no one single way here.

    Where available we pick up input in event form tapped either async on the UI thread (e.g. Android) or once per loop on the main thread (e.g. Windows). Examples are mouse, keyboard, pen, and tablet on Windows and all of Android input. At what resolution these events are created is up to the platform/API.

    Where we have to poll, we try to do so async on a separate thread at a user-controlled frequency. Example: XInput on Windows. Unfortunately, not all APIs that only offer polling allow that.

    Finally, there's some APIs that done async through different mechanisms. E.g. HIDs on Windows have their own async input feed that generally runs at the reporting frequency of the individual devices.

    So yeah, gist is that input *can* be gathered async and *does* retain the granularity of OS events, what that translates to in practice is generally dependent on the specific device/platform combo.
     
    rz_0lento likes this.
  11. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,048
    Fantastic. This is amazing.

    Will you be able to contribute to the PlayerLoop documentation for the newly added system? I hope so... the current docs are extremely thin and your explanations here are like a breath of fresh air...
     
    Ultroman and goncalo-vasconcelos like this.
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    I agree with you, the whole thing needs its documentation rewritten from scratch.

    For the new system I added, I wrote this in the script reference:

    Update phase in the native player loop that waits for the operating system (OS) to flip the back buffer to the display and update the time in the engine.

    TimeUpdate happens at the transition between two different frames.
     
    Ultroman and SonicBloomEric like this.
  13. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    @AnsisMalinsUbisoft, this has landed to 2020.2.0b12. Please see https://forum.unity.com/threads/tim...follow-and-jitter.430339/page-10#post-6438914 on details on how you'd be able to use it.
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    Forgot to update this thread: Android fix landed to 2021.1.0a2 for both OpenGL ES and Vulkan.
     
  15. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    The fix for this landed to 2020.2.0b12 as well. Furthermore, while investigating it I also found that Time.deltaTime broke badly if you went to your driver settings (like Nvidia control panel) and force disabled VSync, while the game thought that it is enabled. So I fixed that too at the same time.
     
  16. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,357
    I'm now assuming Vulkan fix hasn't landed for PC?
     
  17. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,357
    You have any estimate for the multimonitor fix for 2021.1 alphas? :)
     
  18. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    It has not, no.

    2021.1.0a5.
     
    Ultroman, m0nsky and rz_0lento like this.
  19. Shawn-Halwes

    Shawn-Halwes

    Joined:
    Jul 17, 2013
    Posts:
    51
    @Tautvydas-Zilys Can you post the bug link for this issue please? And do you know if this fix was backported to 2019.4 LTS?
     
  20. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    SonicBloomEric likes this.
  21. keeponshading

    keeponshading

    Joined:
    Sep 6, 2018
    Posts:
    916
    Hi. Would it be possible to get an little Git Repro with some Testprojects according to this.

    "Time.deltaTime stability improvements. Frame time calculations are more stable now, providing much smoother object movement when the game is running at a stable frame rate. Unity 2020.2 has time stability improvements on iOS, macOS, PS4, Switch, tvOS, UWP, Windows and Xbox One. Improvements on Android, Windows/Linux on Vulkan and XR will come later. See this forum discussion for more details."

    For me it s a great improvment but it s a little intransparent what´s done, what s possible now and how to use correct.

    In special in multi monitor setups and cinemachine and standard camera jittering following rigid bodys and so on.

    A little Blog Post would be nice too.

    And many thanks to
    rz_0lento

    who backfired this.
     
    Last edited: Dec 15, 2020
  22. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    I wrote a blog post about this a couple months ago: https://blogs.unity3d.com/2020/10/0...020-2-for-smoother-gameplay-what-did-it-take/

    Let me know if you still have questions after reading it.
     
  23. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    272
    cool, but not for android, (VR/Oculus Quest2)

    a realistic assessment of when android will be fixed?
     
  24. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
  25. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    272
  26. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    272
    it's getting better and better..... now nothing works with Unity 2020.2

    > texture browser lags as hell
    > preview packages checkbox for package manager doesn't work! They don't show up!

    enough problems already to not start with that version as well! pfff....

    Guys, UNITY is a mess and by what happened to the company last year, and all the employers who left the company (core developers!!! Whole HQ are empty (see job postings)....

    ....so much for the future oft game developing!
     
  27. xtytyx

    xtytyx

    Joined:
    Dec 29, 2017
    Posts:
    15
    First off i want to say, thank you for all of your work with this project once all the bits get ironed out this will make for some great improvements across the board!

    I seem to be having a problem with any kind of interaction with the 2d pixel perfect camera.

    This is only a problem in 2020.2, in 2020.1 this issue is non existant!

    I have a video showing the problem. when enabled it jitters very badly, and offsets the world relative to the camera

    using fixedUpdate to calculate positions with a low fixedDeltaTime AND having a high framerate <- best option
    (using a low fixedDeltaTime and calculating positions within fixedUpdate was how i was getting around the non consistent delta times previous to these fixes)

    using fixedUpdate to calculate positions with a low fixedDeltaTime AND vsync <- close second

    using deltaTime with high framerate <- third

    using deltaTime with vsync at 60 (basically unplayable) <fourth

    but in all scenarios when pixel perfect camera is enabled we see the characters jump forward and stutter back and fourth

    Again these are only valid when using the 2d pixel perfect camera (as you can see in the video) when pixel perfect is disabled things are smooth (although not pixel perfect)

    I'm not sure if here or if in the URP 2d threads is a better place to post this.

    I also understand this was a huge undertaking and am extremely happy to see it implemented and only wanted to flag this as an issue so it can be known!

    I will also be uploading a bug if this is an unknown issue!
     
    Ultroman and Zullar like this.
  28. MartyMcFly1337

    MartyMcFly1337

    Joined:
    Dec 9, 2016
    Posts:
    2
    On behalf of all Unity devs can I thank @Zullar for pushing this deep issue to a great conclusion from a standing start.
     
    Ultroman, xtytyx, Edy and 1 other person like this.
  29. stereorobo

    stereorobo

    Joined:
    Aug 16, 2018
    Posts:
    1
    Glad to see the fix added to Android, but when can we expect the deltatime fix to make it to Linux as well?
     
    jaymei likes this.
  30. Starburst999

    Starburst999

    Joined:
    May 8, 2017
    Posts:
    23
    Will the fix land on 2020.2 eventually?
     
  31. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    272
  32. Justin-Wasilenko

    Justin-Wasilenko

    Joined:
    Mar 10, 2015
    Posts:
    103
  33. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    272
    I imported my 2020.x project to the 2021.1 beta and made a build. No jittering anymore! Everything runs smooth on the Oculus Quest 2. Finally! Did you made a build with 2021.1, activated phase sync as well, and set the time step to 0.01111? btw multiview/URP. and btw the quest2 is not really a strong device. I still go for render scale 1.0. Because only there you get the full fps. Also I use the Oculus Metrics Tool, to test the framerate on every corner.

    appendix: ups, I see you are aware of all the things xD Now I wonder....
     
    Last edited: Feb 13, 2021
  34. rahul_ak

    rahul_ak

    Joined:
    Mar 17, 2019
    Posts:
    1
    Try Time.fixedDeltaTime instead of Time.deltaTime, it worked for me.
     
  35. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,351
    It does not work, because fixedDeltaTime is the physics framerate, and not related to the display framerate.

    --Eric
     
    Prodigga likes this.
  36. tessiof

    tessiof

    Joined:
    Dec 6, 2017
    Posts:
    25
    Last edited: May 29, 2021
  37. FreeOne

    FreeOne

    Joined:
    Mar 13, 2015
    Posts:
    9
    I'm sorry to "reopen" the topic, but the problem persists in newer versions of unity (2021.2.8f1, 2021.3.0f1, 2022.1.0b16, I also tried the mentioned version 2021.1.0a2) in android builds, although it looks really smooth on windows builds. (new blank projects with squares, moved by transform.Translate())
    Could you recheck it somehow and how can I help?
     
  38. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    I'll ping some folks that work on Android. Did you try tracking Time.deltaTime values? Are they off?
     
  39. FreeOne

    FreeOne

    Joined:
    Mar 13, 2015
    Posts:
    9
    well, bad news: the problem seems to be not in the deltaTime values
    upload_2022-4-15_0-58-26.png

    But the picture (even this graph) "moves back and forward" for a frame like once in 0.5 - 5 sec (and this measurement was also put into empty project).

    And thanks for quick response!
     
    Last edited: Apr 14, 2022
  40. florianpenzkofer

    florianpenzkofer

    Unity Technologies

    Joined:
    Sep 2, 2014
    Posts:
    467
    You could try “Optimized Frame Pacing” in PlayerSettings.
     
  41. FreeOne

    FreeOne

    Joined:
    Mar 13, 2015
    Posts:
    9
    It's already on (by default)
     
  42. FreeOne

    FreeOne

    Joined:
    Mar 13, 2015
    Posts:
    9
    Captured the symptom:
    Movement of 1 object (most noticable at sec 7 to the end):

    To make it more noticable (especially the second half of the video) - movement of background (same script on root bg object):



    Movement code for square (almost the same for backgroud):
    Code (CSharp):
    1.     [SerializeField] private SpriteRenderer _renderer;
    2.     [SerializeField] private Transform _startPosition;
    3.  
    4.     private Vector3 _movementVector;
    5.  
    6.     private void Start()
    7.     {
    8.         _movementVector = new Vector3(5, 0, 0);
    9.     }
    10.     private void Update()
    11.     {
    12.         if (!_renderer.isVisible) transform.position = _startPosition.position;
    13.  
    14.         transform.Translate(_movementVector * Time.deltaTime);
    15.     }
     
    Last edited: Apr 15, 2022
  43. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    I think we will need a bug report on this. Do you know if this repros on any other platform, like Windows builds?
     
    SonicBloomEric likes this.
  44. FreeOne

    FreeOne

    Joined:
    Mar 13, 2015
    Posts:
    9
    nope, it works buttersmooth on windows
    in fact it looks much better even in editor
     
  45. FreeOne

    FreeOne

    Joined:
    Mar 13, 2015
    Posts:
    9
    shall I do something or help somehow?
     
  46. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
  47. FreeOne

    FreeOne

    Joined:
    Mar 13, 2015
    Posts:
    9
    well, I have submitted one two weeks ago but got no reaction yet, shall I post the case number here or smth else?
     
  48. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,892
    Yeah, what's the bug #?
     
  49. FreeOne

    FreeOne

    Joined:
    Mar 13, 2015
    Posts:
    9
    1421680
     
  50. FreeOne

    FreeOne

    Joined:
    Mar 13, 2015
    Posts:
    9
    it looks untouched after a month, can I do something else to help?
     
    invadererik likes this.