Sun脚本RenderProbe在一天的时间

时间:2015-06-11 06:41:46

标签: c# unity3d

我想制作一个统一太阳脚本,在一天的特定时间从阵列中取出光探测器。 这一天是0-1区间(0晚,0.25日出,0.5中午,0。75日落,1晚)

说实话,即使我在达到该值时检查语句currentTimeOfDay == 0.75,也不会发生打印

如何查看多个数组的语句?

   public float secondsInFullDay = 120f;
   [Range(0,1)]
   public float currentTimeOfDay = 0f;
   private float[] floatDay = new float[4] {0f, 0.25f, 0.5f, 0.75f};
   public float timeMultiplier = 1f;



  void Update() {

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

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


    if(currentTimeOfDay == floatDay[0]){
        reflectionProbe.RenderProbe();
        print ("refresh probe");
        }


}

仅打印0值

foreach (float x in floatDay){

    if (x.Equals (currentTimeOfDay)){
        print ("refresh probe");
    }

}

L.E

我设法检查对数组的语句,但它打印多次,这意味着将导致不必要的负载 如果时间timeMultiplier设置为10而不是1,那么当达到该值时打印是一次。

有没有办法将数组与float相乘并获得新数组?

private float[] floatDay = new float[4] {0, 250, 500, 750};

    TimeOfDay = currentTimeOfDay * 1000 * timeMultiplier;
    TimeOfDay = Mathf.Round(TimeOfDay);


    foreach (float x in floatDay){
        if (TimeOfDay == x){
            reflectionProbe.RenderProbe();
            print ("refresh probe");
        }       
    }

L.E 2

修复它,但似乎不紧凑但它有效

private float[] floatDay = new float[4] {0, 2500, 5000, 7500};  

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


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

1 个答案:

答案 0 :(得分:1)

仅打印0值

foreach (float x in floatDay){
    if (x.Equals (currentTimeOfDay)){
        print ("refresh probe");
    }
}

因为浮点永远不准确。您的价值永远不会与您比较的价值完全相等。

请改用:

foreach (float x in floatDay){
    if (x > currentTimeOfDay - 1e-7f && x < currentTimeOfDay + 1e-7f){
        print ("refresh probe");
    }
}

如果仍然无效,请将1e-7f更改为1e-6f,1e-5f等...