3

我试图寻找这个问题的答案,但我想没有人需要这样的东西,或者这是我无法理解的超级简单的东西。所以:

我的值从 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);

希望有人可以帮助我。我对编码相当陌生,我只是想知道这是否可能。谢谢你的时间!

4

6 回答 6

5

我相信与您的解释相匹配的计算将是

newValue = (convertedValue - minHeight) / (maxHeight - minHeight);

newValue = 0@minHeight和 1 @maxHeight

编辑

我以前从未见过 Lerp,但显然它是简单的线性插值。

但是,从MSDN

Lerp 定义为

value1 + (value2 - value1) * amount

即在你的例子中convertedValue应该是分数,答案是插值结果,这意味着你的问题/我的(和 Esailja 的)对它的解释是相反的:)

IE

Mathf.Lerp(10.0, 30.0, 0.5) = 20.0

然而

InvertedLerp(10.0, 30.0, 20) = 0.5 // My / Esailja's calc

:)

于 2012-10-10T11:41:47.263 回答
2

我认为您Mathf.LerpUnity3DAPI 的一部分。已经存在一个功能来做你想做的事情:Mathf.InverseLerp. 你应该使用这个。

于 2012-10-10T12:04:26.563 回答
1
public float minHeight = 10.0f;
public float maxHeight = 30.0f;

float curHeight = 25.0f;

float newValue = ( curHeight - minHeight ) / ( maxHeight - minHeight );
于 2012-10-10T11:42:40.160 回答
0
minvalue=20
maxvalue=45
result=(aktvalue-minvalue)/(maxvalue-minvalue)

像这样的东西?

于 2012-10-10T11:41:19.323 回答
0

房产怎么样?

public int neededvalue
{
    get
    {
        if (value == 45)
            return 1;
        else if (value == 20)
            return 0
        else
            throw new Exception("wrong input");
    }
}

public float neededvaluealternative
{
    get
    {
        return (value - 20) / (45 - 20)
    }
}
于 2012-10-10T11:44:57.877 回答
0

public float AnswerMureahkosQuestion(float input)
{
   const float minValue = 20;
   const float maxValue = 45;
   float range = maxValue - minValue;

   // nominalise to 1
   // NOTE: you don't actually need the *1 but it reads better
   float answer = (input+0.00001/*prevents divbyzero errors*/) / range * 1; 

   // invert so 45 is 0 and 20 is 1
   answer =  1 - answer;
   return answer;
}
于 2012-10-10T11:45:34.283 回答