I am trying to get smoothly from one value to another (from A to B) like Lerp() function does. But if I am correct, Lerp() continuously loops from A to B and than back from B to A and so on… I would like just one time change from A to B and than stop changing the value. I haven’t found any useful functions yet. Do you recommend any ?
Lerp gives a value between A and B. The third parameter is a number between 0 and 1 indicating the mixing ratio of A and B. That’s it. There is no loop, no continuous changing (and no back and forth). However if you want to make a progressive change in time, that is, you want to change continuously from A to B. You need to change the third parameter progressively from 0 to 1.
public static float Lerp(float A, float B, float lerpValue)
{
lerpValue = Mathf.Clamp(0,1,lerpValue);
result = A * (1-lerpValue) + B * lerpValue;
return result;
}
LerpUnclamped is basically the same thing but without the clamp.
Neither of those two functions have the value oscillate back and forth.
What your thinking of is more like Mathf.PingPong()
Required knowledge: IEnumerator Coroutines (frankly required for Unity, lol)
Here are all the variables you need to interpolate a thing. Interpolation takes a lot more than just “a Lerp”:
-A reference to a thing you want to ultimately move.
-The place it starts moving from.
-The place you want it to finish.
-The amount of time it moves.
-The amount of time that has passed.
-The position of the interpolation at a given percentage of that time passed.
You then assign the thing you want to move to the position at a given percent. Those last two can be confusing, because that’s where the curve comes in. The percent will always progress in a linear fashion. The position may not, but always begins and ends with the percent.
EXAMPLE TIEM - This is an academic example. For instance, in practice the startPosition would often be copied from a supplied movingFloat at the beginning of such a function.
Though I am a strong proponent of book learning, a few singular online articles have been indispensable to the advancement of my understanding- so much so that I save them into my library.
Furthermore, I do recommend you initially write your own interpolations. This will eventually encourage you to learn advanced C# concepts as you look for ways to stop writing so much repeat code. Ultimately the plugin DOTween will let you interpolate any variable or even a Transform through space using any curve; with one line of code.
Challenge: Can you make the interpolation perfectly accurate? (hint: use a while() loop and one if() statement)
fucking sweet I got it right in the comment box on the first try
Hi, I think more experienced people have already given good answers, but I might add that there is also MoveTowards, which is pretty straightforward to use, and it’s similar to lerp:
Notice there’s a version of MoveTowards in the .Mathf class also, in case you don’t need a vector.
(also I’m not experienced, just focused on learning first before creating… though I think I named the first project I’m going to finish publicly today!)
Thank you guys for your help! Found a solution. I had to rotate object smoothly to have it faced to a ceratin point on map. Instead of changing only one value (rotation.y), I used Quaternion.Lerp() method, and it works perfectly.