- Home /
Is there a good and easy Way to implement "min Seconds between Playing" when playing an audioSource
I've searched and read many different posts and still haven't been able to accomplish this. So imagine a Rotateable Knob, and whenever it passes a tickMark It plays a Sound. But I also want to implement a minimum time(sound only gets played when passing a mark && a minimum time delay of 0.05 seconds f.e) so that when someone rotates the knob really fast the sound is capped at a maximum ticking speed to sound fairly good even when rotated really fast.
I'm still pretty new to programming so sorry when iam missing something.
Answer by henkehedstrom · Jan 14 at 12:07 PM
On the top of my head you could do something like this:
private const float myMinDuration = 0.5f;
private float myTimer = 0;
void Update()
{
myTimer -= Time.deltaTime;
if(reachedMark && myTimer <= 0)
{
myTimer = myMinDuration;
//Play sound
}
}
I wrote this here in the browser so it is not tested code, might have some syntax errors. I assume you alreaady have the logic to check if something has reached the mark so that is why I just check against a bool called "reachMark". If you have reached the mark and the timer is currently 0 or below you will be allowed to play sound. When we enter the if statement we set the timer variable to 0.5f again so if we reach the mark again before 0.5 seconds has passed the sound would not play.
Your answer
Follow this Question
Related Questions
weapon bobbing 1 Answer
[c#] How can I let something happen after a small delay? 7 Answers
AudioSource.PlayScheduled amount limit? 0 Answers