- I have an a min and max values. My main task is to smooth the value from the min to max through time.
- Once it reaches the maximum I swap min and max and start over again.
Can someone suggest me a good solution for this logic.
Unity has this implemented already.
The Mathf.SmoothDamp() method seems to be what you’re looking for. It gradually smooths a value over time, that value can only be a float though, so if you want to smooth a vector3, you’ll have to do it with all of its 3 components. Check this: http://unity3d.com/support/documentation/ScriptReference/Mathf.SmoothDamp.html
Also I’ve seen a class called AnimationCurve from the unityEngine, I don’t know yet how it works, but seems to be another way to smooth between values using a custom smooth curve.
Also, you may use lerp, but you must take into account that lerp is a linear interpolation (not smoothed). Vector3 can be “lerped” with Vector3.Lerp, no need to do it for all of its 3 components, but as I said, this is not a smoothed translation, just a linear one.
Hope it helped.
You should use math functions, depending on the kind of effect you want.
I often use Mathf.Lerp(minVal,maxVal, progress);
The ‘progress’ parameter is most important here.
You have to calculate the progress through a mathematical function, I’ll give you an example for a simple smoothed progress.
Just use progress = Mathf.Sin(timeProgress);
Timeprogress is a regular counter, but the ‘progress’ value will be smoothed.
Just use that value inside your Lerp and you should have a good effect. There are also other math functions, but this one is certainly usefull.