For with float i returning wrong number.
Hi, I want to make a "For" like this:
for (float i = 0; i <= 1; i+=0.1F)
{
BSMaterial.color = new Color(BSColor.r, BSColor.g, BSColor.b, BSColor.a - i);
yield return new WaitForSeconds(0);
Debug.Log(BSMaterial.color.a);
}
but it gains a Wrong value when I = 0.7, then "I" comes with "0.6900001" and all values after this comes wrong making an infinite loop.
Mathf.Round don't worked for me, since Mathf.Round round a number to 1.
Sorry if I said something wrong, and thanks for helping me.
Looping i like that does not create an infinite loop as demonstrated here.
Floating point accuracy will cause slight differences to values of i compared to what you'd expect. You should google and read about floating point accuracy
In that case my "i" ends like -6.3452e+19, but my next line will sub -0.01F making it an eternity, I found a poor solution, like that:
float alpha = 1;
for (float i = 0; i <= 1; i+=0.01F)
{
alpha -= i;
BS$$anonymous$$aterial.color = new Color(BSColor.r, BSColor.g, BSColor.b, alpha);
yield return new WaitForSeconds(0);
}
alpha = 0;
yield return new WaitForSeconds(3);
for (float i = 0; i <= 1; i += 0.01F)
{
alpha += i;
BS$$anonymous$$aterial.color = new Color(BSColor.r, BSColor.g, BSColor.b, alpha);
yield return new WaitForSeconds(0);
}
alpha = 1;
I don't like that, but I don't know why that don't stop at "-1. something", I googled it and no one says why this doesn't stop.
You can do the loop with an int and calculate the alpha from that.
The way you do things now the fade time depends on framerate. You might also consider tracking Time.time, comparing it to a duration you predefine and calculating alpha from that.
Your answer
Follow this Question
Related Questions
Incremental terrain height increase limited by float size 1 Answer
Calling variables from other script 0 Answers
SetSpeed float C# 1 Answer
Mathf.Round ? [C#] 2 Answers
why the startcoroutine (spawnbigtree()); doesn't work and float don't work 0 Answers