I want a countdown timer to tick faster as the time counts down. the 'tick' is a very short audioclip.
I've got the timer working which calls playTickManager() every frame.
However with this implementation, at some transitions of cancel/re-invoke, the gap between ticks is inconsistent or overlaps causing an unnatural sounding tick.
Is there a better way to implement this without speeding up the actual audioclip, thus maintaining pitch etc?
// This is called every frame by the 'timer' script as it counts down to 0.
// 'seconds' is a float.
void playTickManager()
{
Debug.Log(seconds);
if (seconds >= 20 && tickPlaySpeed != 1)
{
InvokeRepeating("playTickAudio", 1f, 1f);
tickPlaySpeed = 1;
}
else if (seconds >= 16 && seconds < 20 && tickPlaySpeed != 2)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .85f);
tickPlaySpeed = 2;
}
else if (seconds >= 12 && seconds < 16 && tickPlaySpeed != 3)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .7f);
tickPlaySpeed = 3;
}
else if (seconds > 8 && seconds < 12 && tickPlaySpeed != 4)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .55f);
tickPlaySpeed = 4;
}
else if (seconds >= 4 && seconds < 8 && tickPlaySpeed != 5)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .4f);
tickPlaySpeed = 5;
}
else if (seconds > 0 && seconds < 3 && tickPlaySpeed != 6)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .25f);
tickPlaySpeed = 6;
}
else if(seconds <= 0)
{
CancelInvoke("playTickAudio");
tickPlaySpeed = 0;
}
}
void playTickAudio()
{
AudioSource.PlayClipAtPoint(tickAudio, gameObject.transform.position, 1f);
}