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 ?