使一个值的 50% 成为另一个变量的最大值(0%/100% 等于最小值)
Making 50% of a value be the max value of another variable (and 0%/100% equal to minimum value)
我正在使用 Unity 引擎制作游戏,我希望太阳物体的亮度在达到一天中的特定时间之前增加,然后在该时间之后开始减少。
我将白天表示为白天时间的百分比值(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。
我的方法很乱,但我确定可能有一个简单的数学函数?
编辑:只是为了包括我的 "messy" 方式
if(dayPercent <= 0.50f){
currentSunLight = (dayPercent * 2) / maxSunLight * 100;
} else {
currentSunLight = (dayPercent / 2) / maxSunLight * 100;
}
我会建议这样的事情,坚持你目前的逻辑:
currentSunLight = 10 - Math.Abs(dayPercent - 0.5) * 20;
但这是一种线性方法,也就是说,你的太阳 "rises" 线性地然后突然再次线性地落下,就好像它在三角形中移动一样。更好的计算方法是使用三角函数来实现更逼真的场景:
currentSunLight = Math.Cos( dayPercent * Math.PI ) * 10;
我正在使用 Unity 引擎制作游戏,我希望太阳物体的亮度在达到一天中的特定时间之前增加,然后在该时间之后开始减少。
我将白天表示为白天时间的百分比值(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。
我的方法很乱,但我确定可能有一个简单的数学函数?
编辑:只是为了包括我的 "messy" 方式
if(dayPercent <= 0.50f){
currentSunLight = (dayPercent * 2) / maxSunLight * 100;
} else {
currentSunLight = (dayPercent / 2) / maxSunLight * 100;
}
我会建议这样的事情,坚持你目前的逻辑:
currentSunLight = 10 - Math.Abs(dayPercent - 0.5) * 20;
但这是一种线性方法,也就是说,你的太阳 "rises" 线性地然后突然再次线性地落下,就好像它在三角形中移动一样。更好的计算方法是使用三角函数来实现更逼真的场景:
currentSunLight = Math.Cos( dayPercent * Math.PI ) * 10;