Mathf.Lerp crashing build?
Hi,
I am trying to lerp the value of an Image component's fillAmount from it's current slider value to its hard current health value.
healthBar.fillAmount = Mathf.Lerp(healthBar.fillAmount, (float)health.GetHealth()/(float)health.GetMaxHealth(), Time.deltaTime * healthLerpSpeed);
I am running this in fixedUpdate() perfectly with no errors. The strange, unprecedented issue is that it always crashes my Android build when it reaches ~%50 health. I have no idea why. Anyone encounter this before?
Answer by Eric5h5 · Nov 27, 2016 at 11:24 PM
FixedUpdate is for physics; you should be using Update. Also using Time.deltaTime directly in the third parameter of Lerp is not really correct usage and will result in slightly different results depending on framerate. In any case the exact code Unity uses for Lerp is
public static float Lerp (float a, float b, float t)
{
return a + (b - a) * Mathf.Clamp01 (t);
}
There's really no way that can crash anything. If the actual error has anything to do with the code you posted, perhaps GetMaxHealth returns 0 and you get a divide by zero error. Just a wild guess.
Thanks for the FixedUpdate() info, Eric. I don't think Get$$anonymous$$axHealth() would ever return zero- but if it did it would at some point happen in the editor and not just the build. I also put the lerp in a coroutine and monitored correct results. Very perplexing.