I have two numbers, going from 0f → 1f.
They never go past 1f, or below 0f.
Now imagine I want to lerp one of these numbers to the other one, by using the “shortest distance”.
This would not work:
float firstNumber = time1 % 1f;
float secondNumber = time2 % 1f;
float result = Mathf.Lerp(firstNumber, secondNumber, t);
Because if firstNumber is .9f, and secondNumber .1f, the difference would be .8f. But the “smaller” difference would be .3f:
(.3f +.9f) % 1f = .2f
Now, the question is how to write a method that would always return this smaller difference. I feel so confused right now.
Sure, but how would you write a function that gets the .3f in the case above? I.e. how do you “unmodulus” something?
I am not sure I understand. You want to lerp between 2 values but by the sound of it you also want to support wrap around lerping I.E going over 1 goes back to 0?
Something like this:
float firstNumber = time1 % 1f;
float secondNumber = time2 % 1f;
float diff = secondNumber - firstNumber;
float diffWrapAround = (1 - firstNumber) + secondNumber;
if(diffWrapAround < diff)
{
// Wrap lerp
float result = Mathf.Lerp(firstNumber, secondNumber + 1, t);
if(result>1)
result-=1;
}
else
{
// normal lerp
float result = Mathf.Lerp(firstNumber, secondNumber, t);
}
Just a quick hack, no guarantee it works 
1 Like
Yes, exactly, sorry if I wasn’t clear on that. That’s why I have % 1f when setting the variables.
I’ll test your code and get back to you. Thanks!
Alternatively take the CLerp function from here and modify it for specifying your own values.
http://wiki.unity3d.com/index.php?title=Mathfx
1 Like
Thanks a lot!
So nice to see unity employees replying.
Keep it up!
1 Like