3

I'm creating a rhythm game in C# and Unity, and I haven't been able to find a direct correlation between the problems some of my players are having and their hardware but some of them are having problems with the gameplay gradually desyncing from the music over time. It usually takes a couple minutes before it's noticeable, but it definitely happens and ruins the feel of the game.

I'm keeping track of the elapsed time by using a double and adding Time.deltaTime (the time between frames) to it, and I'm assuming that there's just very small precision errors associated with doing that and that's what's causing the gradual desyncs.

Is there a better way to do this that's more accurate? I'm thinking that getting the time that the gameplay started, and then on each frame subtract that from the current time might make it more accurate? I've tried syncing the gameplay time to the audio time (Bass.NET provides a way to get song position in seconds), but while that works for me and quite a few others it ends up making others with decent hardware stutter (my game generally runs just fine on even 8-10 year old PCs).

Ryan Foster
  • 51
  • 1
  • 2
  • You can try using DateTime instead. – Everts Oct 15 '17 at 09:31
  • In Unity `Time.deltaTime` is the most accurate way to do timing stuff. If you can explain how you are using it for your code and post your code, maybe other people can come up with a better way. – Programmer Oct 15 '17 at 12:43
  • I did explain how I was using it, I have a double that I'm adding Time.deltaTime to every frame. – Ryan Foster Oct 15 '17 at 23:07

1 Answers1

1

Depending on your needs, you can use Time.timeSinceLevelLoad, Time.realtimeSinceStartup (although you are advised to use Time.time instead), or most likely Time.time. If you use Time.timeScale to pause your game, you can use Time.unscaledTime.

The difference between Time.realtimeSinceStartup and Time.time is that Time.realtimeSinceStartup will keep on counting even if you pause your game with Time.timeScale. Assuming that users might pause your game, which may or may not stop your audio from playing, would help you strategize whether you are going to use which property. You may want to set Application.runInBackground to true to get most reliable results, depending on your game logic.

Serkan Pekçetin
  • 658
  • 7
  • 14