In Unity 2022.3.12f1, I made a metronome. Here’s a minimal working instance:
utilizing System;
utilizing UnityEngine;
public class Metronome : MonoBehaviour
{
public int tempo = 150;
public int tempoReference = 4; //e.g., 4 means reference is 1 / 4 word, 2 means reference is a double word,...
public int timeSignatureTop = 4;
public int timeSignatureBottom = 4;
public AudioSource supply; //You must fill this with any quick sound
non-public float delayBeforeNextBeat;
non-public float beatDuration = 0;
non-public bool play = false;
non-public int beatIndex = 0;
public void Begin()
{
supply.Play();
beatDuration = 60f / (timeSignatureBottom / tempoReference * tempo);
delayBeforeNextBeat = beatDuration;
beatIndex = 0;
play = true;
}
non-public void Replace()
{
if (play)
{
delayBeforeNextBeat -= Time.deltaTime;
if (delayBeforeNextBeat <= 0)
{
beatIndex = (beatIndex + 1) % timeSignatureTop;
delayBeforeNextBeat = beatDuration + delayBeforeNextBeat;
supply.Play();
}
}
}
}
My drawback is that the interval between the primary two beats (as heard by the AudioSource
) is for much longer that the subsequent ones. Further prints point out that Time.deltaTime
s are usually not significantly longer nor extra quite a few on this interval than they’re in others, although.
I attempted to maneuver every thing in FixedUpdate
, with the identical consequence.
I additionally tried to maneuver the primary supply.Play()
contained in the Replace
operate (with some minor changes, like including a bool firstPlay
and initializing beatIndex
to -1), with the identical consequence.
Does someone have an concept?