I’ve 1 variable (test) with a value of 1. When the value of the variable changes to 4, I want to Lerp to that new value over time. Does any one know how to do this?
This is wat I’m trying to do:
var b = Mathf.Lerp(test_old_value, test_new_value, Time.time)
What you’ll need to do is store not only the original point and the end point (here, minimum and maximum), but the duration you want the Lerp to take place over, as well as how far along the process you are.
Here’s the basics. I’m assuming you want to increase it by deltaTime every update, and currently it’ll go past 1.0, so you’ll need to fix that…
var minimum = 10.0;
var maximum = 100.0;
var timeLimit = 10;
var currentTime = 0;
var key = 0.0;
function FixedUpdate(){
print(key);
currentTime += Time.deltaTime;
key = Mathf.Lerp(minimum, maximum, currentTime / timeLimit);
}
Personally, I’d just cop-out and use iTween seeing as it covers a basic lerp transition as well as the other Robert Penner calculations, but that is just me.
@saurabh, @Kayn; Thanks for your help! It did not help to solve my problem but it gave me some new insight to get it done. I’m using now a smootfollow script to get thing done. Thanks!
Hi Frido, i was just going through the unity forums and read your post.
Did you get the effect you wanted?
What i understood from this thread that you want when the value of that variable via lerping reaches 4 you want to call another functionality. (Correct me if i am wrong)
You could try this with Mathf.MoveTowards. I also wanted the same kind of functionality in one of my games and i achieved that with Mathf.MoveTowards. Actually lerping never gives you exact values whereas MoveTowards make sure that the value never exceeds max and min and restricts it to the same value of the bounds.
I think the original question was more “how does Lerp work” So he says he has a value of 1, and he wants to Lerp it to 4.
Mathf.Lerp(value1, value2, value3);
Lerp by definition is a function with gives a result betwen value1 and value2 at a timeframe of value3. Value1 and 2 (in this case are floats) and Value3 is a float between zero and one. (0 - 1)
Givin this, Mathf.Lerp(1.0,4.0, 0.5) is half the distance between 1 and 4 or 2.5.
One of Lerp’s favored functions is a smoothing over time effect. Which looks like this:
current = Mathf.Lerp(current, desired, step);
So if we set the current to 1 and the desired to 4, we can “step” a percentage of difference to the desired. So if your step is 0.2, it steps 20% of the amount that is left over every frame. Do not get 20% as the overall percentage of distance though, it is 20% of the remaining amount, so you really never get to the desired amount.
You would make a coroutine for this; Update is unsuitable since it runs every frame, and the desired result is a specific start and end. Assuming a global variable called “test”, you would call this coroutine when you want test to lerp from the start to end values over a given number of seconds:
function LerpNumber (start : float, end : float, time : float) {
var t = 0.0;
var rate = 1.0 / time;
while (t < 1.0) {
t += Time.deltaTime * rate;
test = Mathf.Lerp(start, end, t);
yield;
}
}
This could be made better and more flexible if you were able to pass in an arbitrary float rather than using a specific global variable. Unfortunately this would require declaring a reference variable in the function, which currently can’t be done in JS, but even if you could, it wouldn’t help, since coroutines can’t use reference variables anyway, so this can’t be done in C# either. One somewhat clumsy work-around I can think of would be to make a custom class that consists of just a float, since classes are always passed by reference regardless. So you’d pass in a variable made from that custom class instead of a standard float.
This is totally doable in Update. You have to be a little more blunt about it though.
var t=0.0;
var start : float =1.0;
var end : float = 5.0;
var time : float = 3.0;
function Update() {
var rate = 1.0 / time;
t += Time.deltaTime * rate;
test = Mathf.Lerp(start, end, t);
}
Actually I said that Update is unsuitable, not that it can’t be done. Your code runs every frame, so it lerps whether you want it to or not, and continues running uselessly after t > 1.0. You can add checks to prevent this, but then the checks have to be run every frame. It’s just a lot simpler to use a coroutine, which runs only when you say and stops running when it’s done, and doesn’t require any other logic.