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

Time.time vs Time.fixedTime

Discussion in 'Scripting' started by Wasiim, Jun 11, 2015.

  1. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    what is the difference or relationship between these 2.
     
    unity_OqOXkXTI8Pjm8Q likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Have you tried reading the documentation for the two properties? Do you have a more specific question about them?
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
  4. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Also important to note:

    FixedUpdate happens at the same rate every single frame. That is why it is generally used for physics. Update does not.

    This is important to note because Time.fixedDeltaTime uses whatever that physics update rate is. As lordofduct said above, it's just a read/write property.
    Regular Time.deltaTime uses the time between update calls, which isn't necessarily the same every frame at all since different things affect it.
     
  5. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    I did read the documention like always but its always confusing.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Which part was confusing?
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    um... he said Time.time, not deltaTime... not sure if this was a mistake. anyhoo

    Time.deltaTime = Time delta (difference) between frames
    Time.fixedDeltaTime = Physics time step
    Time.time = Total time since game started (game timer if you will)
     
  8. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    Oh wow, good call. I was going based off what lordofduct wrote instead of the actual title of the post.
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Nobody actually answered the question yet! To be fair, I can see why you all are assuming that the OP is asking about fixedDeltaTime, since I haven't actually ever seen anybody use fixedTime (had to double-check that it actually exists), but still.

    Now, Time.time and Time.fixedTime are pretty well documented, but you have to understand how Update and FixedUpdate frames work to understand the docs.

    The short version is this:

    Time.time is the last point at which the engine started running Update on all your scripts.
    Time.fixedTime is the last point at which the engine started running FixedUpdate on all your scripts.

    As a further complication, if you are inside a FixedUpdate function, Time.time returns Time.fixedTime instead. This is consistent with Time.deltaTime, which gives you Time.fixedDeltaTime inside FixedUpdate.

    The long version (still somewhat simplified, and based of heuristics and hearsay):

    The underlying physics calculation of Unity is a simulation. It doesn't have its internal "clock", instead, Unity asks it to simulate that a certain amount of time has passed - let's call this simulation step a physics 'tick'. The amount of time simulated in each of these ticks is a value called the fixed timestep, which is a value you can set for your game.

    The FixedUpdate function is run in sync with these ticks - so per tick, you get one FixedUpdate. I believe that FixedUpdate happens directly after the tick (as opposed to directly before), but I'm not completely sure about that.

    The Update function, on the other hand, runs as often as Unity manages to call it, and is synced to Unity drawing everything on your screen. The rate of your Update function is roughly equivalent to what you generally call Frames Per Second - fps (not related to First Person Shooter in any way).

    After Unity calls Update, it checks how long it is since it called FixedUpdate last. If that time is larger than the fixed timestep - the wanted tick interval - it calls FixedUpdate. In fact, it runs FixedUpdate enough times to catch up to where the physics simulation "should" be. So if the last time FixedUpdate was run was at 2.00 seconds, the fixed Timestep is at 0.02 seconds, and Update finished at 2.11 seconds, FixedUpdate will be called 5 times, until the physics engine has simulated up to 2.10 seconds.

    Note that all Update and all FixedUpdate functions on all your MonoBehaviours are called in one go. The value of Time.time in any of those Update function is the "real" time at which Unity started calling the first of them. Similarly, Time.deltaTime is the "real" time between this round of Updates, and the last round of Updates.

    The value of Time.fixedTime within each of those FixedUpdate functions, though, is the time the physics engine were simulating when those FixedUpdate functions were called. Similarly, the value of Time.fixedDeltaTime is the duration of the simulated tick - ie. 0.02 seconds if your fixed timestep is 0.02. In essence, Time.fixedDeltaTime never changes.

    Final note: I'm writing "real" time under Update since it's not actually real time - if you change the value of Time.timeScale, Time.time respects that. So if your timeScale is 2, Time.time increses twice as fast, while with timeScale at 0.5, Time.time increases half as fast.

    If you need the actual real time since the game started, use Time.realTimeSinceStartup, which uses your system's internal clock.

    Hope that's informative!
     
    Last edited: Jun 12, 2015
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Heh, just noticed he did say time and fixedTime. Dyslexia must have been in high gear yesterday.
     
  11. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    I still do not get it but i should stop coming to this forums for i have figured out its not for me and 9/10 times i don't find my answers here but from some external source whatever that may be...(google? nah don't think so more like you tube well google does own youtube so i guess you can say google)
     
  12. Deleted User

    Deleted User

    Guest

  13. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    You sound like you might be a visual learner. Rather than learning from text.
     
  14. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    yeah...your time was not wasted @Baste
     
  15. Deleted User

    Deleted User

    Guest

    Isn't text something to see? :p (just kidding)
     
  16. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    I was being serious. Some people are just better at learning visually.
     
  17. Deleted User

    Deleted User

    Guest

    I know I was making a joke and saying text is something you see. :)
     
  18. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    lol i love that joke, i will steal that.:p
     
    Deleted User likes this.
  19. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Actually, this question comes up so many times that I figured it'd be nice to have a proper write-up I can refer back to.
     
  20. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    real slick...
     
  21. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Sounds like you're an artist.
     
    Baste likes this.
  22. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Excellent job. This should be stickied.
     
  23. unity_OqOXkXTI8Pjm8Q

    unity_OqOXkXTI8Pjm8Q

    Joined:
    Dec 21, 2020
    Posts:
    1
    Thanx for taking the to explain it