How would you execute code every X angles? To provide some context, in the VR game I’m working on, I want to play an audio file every 30 degrees that a crank turns. When using my current code, turning the crank slowly works mostly fine, but turning it quickly results in it missing certain angles, so that it doesn’t play the audio file reliably.
Below is a snippet of the code in my Update function (not fixed update).
if(drum.angularVelocity.z < -1)
{
if((int)drum.rotation.eulerAngles.z % 30 == 0)
{
if(shouldPlayClick)
{
if(clickCount > crankClicks.Length -1)
{
clickCount = 0;
}
crankAudio.clip = crankClicks[clickCount];
crankAudio.Play();
clickCount++;
shouldPlayClick = false;
}
}
else
{
shouldPlayClick = true;
}
}
Any ideas? Am I going about the problem wrong, or is there something I can add to my code to make it more reliable?