public static float evaluateProbabilityCurve(AnimationCurve curve, int sliceCount)
{
/*
allows you to use animationCurves as probability curves.
uses domain 0-1
any range
slices the domain into sliceCount pieces. the odds of returning the x value from a slice
is linearly proportional to the y value on the curve
*/
float total = 0;
float stepSize = 1 / (float)sliceCount;
for (float x=0; x<=1; x+=stepSize) {
total += curve.Evaluate (x);
}
float rand = ((float)Random.Range (0, total * 1000)) / 1000;
for (float x=0; x<=1; x+=stepSize) {
float y = curve.Evaluate (x);
if (y > 0)
rand -= y;
if (rand < 0)
return x;
}
Debug.Log ("warning: evaluateProbabilityCurve never evaluated. returning 1");
return 1f;
}