Monday, January 1, 2018

unity - How to synchronize a game with music


You may be familiar with ryhtm games as Dancing Line, Geometry Dash, or even Guitar Hero. They have something in common : every single action in the game is synchronized with the music. My purpose is to make a game like Geometry Dash where the player has to do some actions in rythm while the character scrolls automatically in the level. I have a few questions about this type of game :



  • Do I have to keep my FPS constant as it has to be always synchronized with the music ?

  • How can I build my levels with a music in mind to make sure that it will be synchronized ?


I am working on Unity using C#.



Answer



I don't recommend using Time.deltaTime to track progress through playing the song as described in another answer.


It's great for smoothing & interpolating effects to play out in realtime, but when you want to match the song's time you should ask the song.



In Unity, you can measure an Audio Source's progress through playing a track directly using AudioSource.time or AudioSource.timeSamples to get the precise playhead position. (Note that the latter measures in samples, which may vary in rate per second depending on how you've compressed your track)


This way, you don't need to run your own separate timer and hope they stay in sync. You can wire up game triggers of beat matching effects to run off of the song's own timeline.


For layering gameplay sounds onto the music, you can use AudioSource.PlayScheduled to ensure your sounds land exactly on the beat, as even a small mismatch can be noticed


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...