#Unity Rhythm Game Timing

1 messages · Page 1 of 1 (latest)

wheat holly
#

Hello people! It's been a bit of while since I've been here, and for some reason I cannot really wrap my head around how to make note spawning and timing accurate in Unity, as some notes either spawn too late, e.g:

#

when realistically I want them like this:

#

My code for spawning notes is something like this:

if (GetAudioSourceTime() >= timeStamps[timeStampIndex] + noteOffset)
                    {
                        Debug.Log("trying to load a damn note");
                        jsonLanes[laneStamps[laneStampIndex]].CreateNote(timeStamps[timeStampIndex]);
                        timeStampIndex++;
                        laneStampIndex++;
                        currentLine++;


                    }```
#

GetAudioSourceTime() is simply just cs return instance.song.time

#

Can anyone enlighten me on some ways to make note spawning more accurate?* I've even tried making GetAudioSourceTime return:

return instance.song.timeSamples / instance.song.clip.frequency```, but this yields a slightly more accurate result, although still noticeably off.'
keen umbra
#

How are your note positions calculated?

wheat holly
#

like, where they spawn?

keen umbra
#

No, like how their position changes as time passes.

wheat holly
keen umbra
#

How's t obtained?

wheat holly
#

oh t is ``` t = (float)(timeSinceInstantiated / JSONBeatmapLoader.instance.NJS * 2);

#

timeSinceInstantiated is obtained through storing GetAudioSourceTime() when the note is spawned

#

NJS is just note speed (something like 1.875 right now)

keen umbra
#

Well there's your problem

#

Imagine your game lagged, and you note spawned 10 ms late, then it will start at the starting position 10 ms late, and reach the end position 10 ms late.

#

In a rhythm game, timing is not managed like this.

wheat holly
#

when you put it like that, that makes sense

#

if the answer is what i think it is, surely i can just replace assign timeSinceInstantiated as the timeStamp that the note should be spawned at, right?

keen umbra
#

Basically yes.

#

But to get more in depth with how timing is managed in a rhythm game

#

You first obtain a sync point between your audio time and game time ("audio time 1234 is game time 5678")

#

Then your entire game is driven by game time and game time only.

#

Your beatmap requires a note to spawn at audio time x, then use the sync point to calculate the corresponding game time, and then everything from then on operates solely in game time.

#

A subtle detail is that you do not use audio time as the driver of the game, but rather game time (more specifically, game time at start of the frame)

#

This is because depending on audio engine, audio time can still progress in middle of the frame, causing the notes that are updated later in the same frame to possibly shift.

wheat holly
#

oh yeah, that's a thing isnt it? i forgot