我基本上想檢查newValue是否超過targetValue。但是targetValue可能是正數或負數,所以如果(newValue < targetValue)
不一定有效。這個正面/負面的支票可以簡化嗎?
我編寫它下面的方式,我可以在這裏得太多的東西,但我不知道是否有改寫的方式,如果檢查多一點優雅......
var newValue = 0;
function ChangeValue (targetValue : int)
{
var isTargetPositive = (targetValue > 0);
if (isTargetPositive && newValue < targetValue || !isTargetPositive && newValue > targetValue)
newValue = math.moveTowards(newValue, targetValue, 1);
else
// Do something else
}
任何你初始化「NEWVALUE」零,所以如果「targetValue」大於零,「NEWVALUE」將* *總是**小於「targetValue」。 – Pointy
'newValue
Blender
該代碼無法正常工作。 –