- Home /
Mathf.PingPong with ease in and out?
I have the following code in Update() which counts from 0 to 100 and back to 0, but I want to make the value start slowly, build up to speed as it increases, then slow as it reaches 100, and then speed up as it starts back to 0, and again slow as it reaches 0.
I need to use the value to ease in and out from 0 to 100
Debug.Log(Mathf.PingPong(Time.time * 10, 100));
I have looked at lots of the Mathf functions but no joy... anyone able to help?
Neither examples are giving me what I want? Hermite is closet, but the ease in and out is so slight it is hardly noticeable, is there a way to define the ease in and out amount, the effect I want is like a bouncing ball.
In fact what I really need is my value to go 0 to 100 the first time, then 0 to 80 the next time, and then 0 to 60 etc, etc...
@greg-bassett: Don't post comments as answers. Answers should answer the question. What you describe in your comment is not really what you said in your question. You said you want to ease in and ease out. a bouncing ball just eases at one end. At the bottom it actually has the highest speed when it returns.
It's still not clear what you really want. If you want to reduce the max value each cycle it needs some reference since Time.time just keeps growing. If you want to simulate physics, why don't you use physics? At least what you describe is not really a usual interpolation function. Or can you draw the graph of your position function?
Answer by Eric5h5 · Jul 30, 2013 at 10:32 PM
Mathf.SmoothStep(0, 100, Mathf.PingPong(Time.time/10, 1))
Just had a look and SmoothStep is just a hermit polynom:
public static float SmoothStep(float from, float to, float t)
{
t = $$anonymous$$athf.Clamp01(t);
t = -2f * t * t * t + 3f * t * t;
return to * t + from * (1f - t);
}
Yep, I used to write code which did that before they implemented SmoothStep. A $$anonymous$$or thing but it's kind of nice to have it built-in, since I tend to use hermite interpolation mostly.
Answer by Bunny83 · Jul 30, 2013 at 10:20 PM
Well, there are many ways to do this. One is to use Mathf.Cos along with Math.PingPong.
Something like that:
// C#
(Mathf.Sin(Mathf.PingPong(Time.time*Mathf.PI, Mathf.PI))+1)*0.5f*100f
Another way is a Hermite interpolation:
float Hermite(float t)
{
return -t*t*t*2f + t*t*3f;
}
Hermite(Mathf.PingPong(Time.time,1.0f))*100f
Your answer
Follow this Question
Related Questions
Fade 3d text with lerp? 1 Answer
Easy fade in/out on level load/end? 2 Answers
How to fade in GUI text. 2 Answers
Getting In And Out Of Cars With Jcar 0 Answers
Fade In and out when level is loaded 2 Answers