检查float对阵列但跳过值

时间:2015-06-11 08:29:32

标签: c# unity3d

我有这个脚本从时间开始对reflectionProbe.RenderProbe()执行固定值,问题是它有时会从数组中跳过一个值。我做错了什么?

http://i.imgur.com/9NwVq7X.png

public float secondsInFullDay = 120f;
private float[] floatDay = new float[40] {
    0, 250, 500, 750, 1000, 1250, 1500, 1750, 2000,
    2250, 2500, 2750, 3000, 3250, 3500, 3750, 4000, 
    4250, 4500, 4750, 5000, 5250, 5500, 5750, 6000,
    6250, 6500, 6750, 7000, 7250, 7500, 7750, 8000,
    8250, 8500, 8750, 9000, 9250, 9500, 9750};

[Range(0,1)]
public float currentTimeOfDay = 0f;

public float TimeOfDay = 0f;
public float timeMultiplier = 1f;

void Update() {
    currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;

    if (currentTimeOfDay >= 1) {
        currentTimeOfDay = 0;
    }

    TimeOfDay = currentTimeOfDay * 10000 / timeMultiplier;
    TimeOfDay = Mathf.Round(TimeOfDay);


    foreach (float x in floatDay){
    float y = x / timeMultiplier;
        if (TimeOfDay == y){
            reflectionProbe.RenderProbe();
            Debug.Log ("refresh probe " + y);
        }       
    }
}

2 个答案:

答案 0 :(得分:1)

if(Mathf.Approximately(TimeOfDay, y))
{
    //Do something
}

答案 1 :(得分:0)

很可能你的浮点有问题。

调试记录所有浮点值(x,y,TimeOfDay)

我怀疑y是有问题的

很可能你必须使用:

if (TimeOfDay > y - 1e-7f && TimeOfDay < y + 1e-7f)

而不是

if (TimeOfDay == y)