Syncing music with movement

For several days in my rhythm game im trying to get the level with the music synced, but doesnt matter what im trying there is always a delay in music or movement from clip to clip.

I have a gameobject with a rigidbody attached, inside are gameobjects with colliders. I am moving the rigidbody downwards and the player needs to press a button at the correct time based on the rhythm given from the music when a collider reaches the button.

The most logical approach i tried:

beatTempo = 140f;  // songs beattempo
songLag = 0.35f;  // time in second until the first beat starts
songLagStep =  (beatTempo / 60) * songLag;
velocity = beatTempo / 60f;
velocityAdd = new UnityEngine.Vector3(0f, -velocity, 0f);
startDelayedTimer = 0f;
startDelayedBy = 0.998f;  // -2ms to assure the fixedupdate is triggered at 1f not 1.02f
beatMusic.PlayScheduled(AudioSettings.dspTime + (double)(startDelayedBy + 0.002f));

private void FixedUpdate()
    {    
        if (startDelayedTimer >= startDelayedBy)
        {
            tabObjectsAll.gameObject.GetComponent<Rigidbody>().MovePosition(tabObjectsAll.gameObject.GetComponent<Rigidbody>().position + (velocityAdd * Time.fixedDeltaTime));
         }
        else
        {
          
             if(startDelayedTimer == 0f)
            {
                    tabObjectsAll.transform.position = new UnityEngine.Vector3(0f,0f + songLagStep,0f);
             }
              startDelayedTimer += Time.deltaTime;
         }
    }

In reality the rigidbody is child of tabObjectsAll, simplified the code above…

Can anyone tell me what i did wrong ?

I’m not really familiar to audios, but if you want better timing, maybe you can use AudioSource.timeSamples to check what time is now, or use MonoBehaviour.OnAudioFilterRead to get more informations. (I’m not sure what those really represents, but since they are audio-relative, I believe these info may help you)
One thing I can tell is that Audio is played on another thread , so it will always be some time difference if you use normal Update (or FixedUpdate) functions. You may have to eliminate those difference by calculation.

1 Like