Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Time.time vs Time.fixedTime

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

  1. matriXcel

    matriXcel

    Joined:
    May 2, 2015
    Posts:
    211
    what is the difference or relationship between these 2.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,822
    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:
    6,741
  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. matriXcel

    matriXcel

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

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,741
    Which part was confusing?
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    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:
    4,337
    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:
    6,741
    Heh, just noticed he did say time and fixedTime. Dyslexia must have been in high gear yesterday.
     
  11. matriXcel

    matriXcel

    Joined:
    May 2, 2015
    Posts:
    211
    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:
    6,741
    You sound like you might be a visual learner. Rather than learning from text.
     
  14. matriXcel

    matriXcel

    Joined:
    May 2, 2015
    Posts:
    211
    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:
    6,741
    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. matriXcel

    matriXcel

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

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,337
    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.
     
    matriXcel and Deleted User like this.
  20. matriXcel

    matriXcel

    Joined:
    May 2, 2015
    Posts:
    211
    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.