我正在使用Unity Engine进行游戏,我希望太阳对象的亮度增加直到到达一天中的特定时间,然后在该时间之后开始减小。
我将白天表示为白天的百分比值(0到1),如下所示:
float currentTime = 0.50f; //50% = noon, 0%/100% = midnight
float dayRange = 0.75f - 0.25f; //Daylight hours = 25% to 75% (6:00 to 18:00)
float dayPercent = (currentTime - 0.25f) / dayRange; //Current time between the daylight hours as a percentage.
float maxSunLight = 10f;
float currentSunLight = 5f;
我想要的是:
当
dayPercent
在0到0.5之间时,currentSunLight
将从0增加到10。当
dayPercent
在0.5和1之间时,currentSunLight
将从10减少到0。我有一个麻烦的方法,但是我敢肯定有一个简单的数学函数可以做到这一点?
编辑:只是包括我的“凌乱”方式
if(dayPercent <= 0.50f){
currentSunLight = (dayPercent * 2) / maxSunLight * 100;
} else {
currentSunLight = (dayPercent / 2) / maxSunLight * 100;
}
最佳答案
我会建议您遵循当前的逻辑,例如:
currentSunLight = 10 - Math.Abs(dayPercent - 0.5) * 20;
但这是一种线性方法,这意味着您的太阳呈线性“升起”,然后突然又线性线性化,就像它正在以三角形运动一样。一种更好的计算方法是使用三角函数来实现更逼真的场景:
currentSunLight = Math.Cos( dayPercent * Math.PI ) * 10;