What is the most accurate way to call a function based on time?
I'm making a sort of music game where you need to spin a crank with the BPM. I was originally using a simple Time.deltaTime timer on every update, but after trying to sync it with some music I noticed it was quite off most of the time. I've changed the code to InvokeRepeating but the timing is still off. How my code works is basically:
Stopwatch timer = new Stopwatch();
void Start()
{
G.beatDelay = 60 / BPM; //this gets the time between 2 beats on a given BPM
timer.Start();
InvokeRepeating("BPMTick", G.beatDelay, G.beatDelay);
}
void BPMTick()
{
sfx.Play(); //play quick metronome sound
Debug.Log(timer.ElapsedMilliseconds); //showing me how accurate the timing is
timer.Restart();
G.beat = true;
}
Essentially, set the variable G.beat to true when a beat happens, set it back to false the next frame. But my biggest problem is executing this code accurately. InvokeRepeating is the best option I know, but it still has a delay of around +4ms and sometimes can do -5ms or +20ms, which makes the metronome sound wonky. The difference also adds up over a few minutes or so. I also tried using FixedUpdate but got very similar results.
Is there any way to execute the code with a + or - 1ms difference? It seems a lot to ask but maybe you C# magicians know how to do this. Thanks!
Your answer
Follow this Question
Related Questions
Displaying C# Int Value to GUI 2 Answers
How do I control the camera with my mouse? 0 Answers
Why the values in the hierarchy and the values in the code are different ? 0 Answers
Struggling to get the rotation the player is moving in. 1 Answer
Rotation starts freaking out when I put my cursor on it. 1 Answer