I have a float value that I’m trying to have trigger a sound event whenever it crosses past an integer threshold. The user is able to increment the value in 0.1 increments.
In other words, whenever the value equals the nearest integer, I want to play a sound… so when the user takes it from 3.1 to 3.0, BING. and when they take it from -1.9 to -2.0, BING! and when they take it from 0.9 to 1.0, BOP!, and when they take it from -4.1 to -4.0, BOP! It seems that somehow my internal math manipulations are also slipping float values from time to time, transforming 3.1 into 3.0999999, so a solution adjusting for that strange variance would also be greatly appreciated.
Here’s the code that works 30% of the time:
var g_MC = new Vector4[n];
function chargeDown(h) {
g_MC<mark>.w -= 0.1;</mark>
if (Mathf.Abs(g_MC.w) - Mathf.Round(Mathf.Abs(g_MC.w)) < 0.01) {
AudioSource.PlayClipAtPoint(g_sFXchargeDown, Vector3(g_MC.x, g_MC.y, g_MC.z));
Debug.Log(“CHARGE DOWN ---- BING!”);
}
}
function chargeUp(h) {
g_MC.w += 0.1;
if (Mathf.Abs(g_MC.w) - Mathf.Round(Mathf.Abs(g_MC.w)) < 0.01) {
AudioSource.PlayClipAtPoint(g_sFXchargeUp, Vector3(g_MC.x, g_MC.y, g_MC.z));
Debug.Log(“CHARGE UP ---- BOP!”);
}
}