1

According to the Unity documentation, FixedUpdate get called in a fixed time step. This can be seen when we output the time.Deltatime in FixedUpdate call back.

However, when I tried to check the deltatime using .Net time system (i.e., TimeDate.now.millisecond) in FixedUpdate call back, the output of deltatime is not constant.

I suspected that the clock used by Unity3d is not the same from my PC's clock. But I am not sure if I am right or not. Does anyone know the reason about this?

Thanks

Note: the main question is about why capturing the interval time between consecutive FixedUpdate() using .NET DateTime.now produce the inconsistent time step while it shows consistent time step using Time.Deltatime.

deduu
  • 157
  • 5
  • 12
  • 2
    Welcome to the world of non-real-time operating systems. Code takes time to execute, depending on processor/thread scheduling by the OS. Very rarely does anything actually happen exactly on-time. Windows and most linux just aren't that kind of operating system. – spender Mar 10 '16 at 14:04
  • 1
    As a note, if you are attempting to time things with precision, `StopWatch` is preferable as it may have higher precision than `DateTime.Now`. – Glorin Oakenfoot Mar 10 '16 at 14:07
  • 2
    DateTime has a resolution of aprox. 15 ms... it's not usable for games: http://stackoverflow.com/questions/2143140/c-sharp-datetime-now-precision – Gusman Mar 10 '16 at 14:08
  • (1) it has no connection whatsoever to what you think. (2) never, ever, ever use these functions, for any reason. Ever – Fattie Mar 10 '16 at 15:02
  • Possible duplicate of [Get current time on Android device](http://stackoverflow.com/questions/32280589/get-current-time-on-android-device) – Fattie Mar 10 '16 at 15:04
  • @GlorinOakenfoot, Yes, I've tried implementing Stopwatch. it seems to have higher precision than DateTime.now, but I guess it is still not good enough for the small timestep (e.g., 20 ms). :) – deduu Mar 10 '16 at 16:37
  • @JoeBlow can you give any reason/evidence? – deduu Mar 10 '16 at 16:38
  • @deduu i encourage you to start learning everything there is to know about frame and coroutines. be sure to *please vote "up"* essay-answers you read along the way ! :) http://stackoverflow.com/a/35651128/294884 or for example http://stackoverflow.com/a/35280256/294884 – Fattie Mar 10 '16 at 16:58
  • note that on questions like yours here, it is often very worth **mentioning what you are *actually trying to do*** ... it can dramatically simplify providing an answer. it is extremely common in Unity that people begin addressing a problem in totally the wrong milieu in Unity. – Fattie Mar 10 '16 at 16:59

1 Answers1

0

Please check this.

FixedUpdate: FixedUpdate is often called more frequently than Update. It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high. All physics calculations and updates occur immediately after FixedUpdate. When applying movement calculations inside FixedUpdate, you do not need to multiply your values by Time.deltaTime. This is because FixedUpdate is called on a reliable timer, independent of the frame rate.

And also in image you can read this near FixedUpdate State:

The physics cycle may happen more than once per frame if the fixed time step is less then the actual frame update time.

If you need to check time in FixedUpdate you should use Time.fixedDeltaTime.

Barış Çırıka
  • 1,570
  • 1
  • 15
  • 24
  • Hi Baris, Actually, to check the time interval using Time.Deltatime in FixedUpdate() will return the same value as Time.fixedDeltaTime. – deduu Mar 10 '16 at 16:51
  • 1
    OP: *"the main question is about why capturing the interval time..."* your code is only running *somewhere within the frame or within the pseudoframe*. Why would there, or could there, possibly be any relationship whatsoever between the gaps between such happenings? – Fattie Mar 10 '16 at 17:02
  • "FixedUpdate is often called more frequently than Update. It can be called multiple times per frame, if the frame rate is low and it may not be called between frames." is the answer. – Barış Çırıka Mar 10 '16 at 17:57