我试图寻找这个问题的答案,但我想没有人需要这样的东西,或者它非常简单,我只是无法理解。所以:
我有一个从 45 变为 20 的值。
我需要一个在 45 到 20 的同时从 0 到 1 的值。我知道 45 - 20 = 25 那将是我的 100%,因此是数字 1。
我会像这样在 Lerp 值中实现它:
public float minHeight = 10.0f;
public float maxHeight = 30.0f;
public float convertedValue;
转换值 = ??? (类似于 45 - 20 = 25 = 100%)* 0.01;
newValue = Mathf.Lerp(minHeight, maxHeight, convertedValue);
希望有人能帮助我。我对编码相当陌生,我只是想知道这是否可能。感谢您的宝贵时间!
最佳答案
我相信符合您的解释的计算是
newValue = (convertedValue - minHeight) / (maxHeight - minHeight);
即newValue = 0
@minHeight
和 1 @maxHeight
编辑
我以前从未见过 Lerp,但显然它是简单的线性插值。
但是,来自 MSDN
Lerp 定义为
value1 + (value2 - value1) * amount
即在你的例子中 convertedValue
应该是分数,答案是内插结果,这意味着你的问题/我的(和 Esailja 的)解释是倒置的:)
即
Mathf.Lerp(10.0, 30.0, 0.5) = 20.0
鉴于
InvertedLerp(10.0, 30.0, 20) = 0.5 // My / Esailja's calc
:)
关于c# - 如何让值从0到1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12818440/