1

I have created a helper to PingPong from 1 to 0, but I'm having a hard time inverting this, so that the value goes from 1 to 0.

Below you can see the code, I'm currently using. I'm also not sure if this is even possible.

_lerpPulse = PingPong(Time.time * 0.5f, 0f, 1f);

And the helper

float PingPong(float aValue, float aMin, float aMax)
{
    return Mathf.PingPong(aValue, aMax - aMin) + aMin;
}

Thanks in advance!

Matt Breckon
  • 3,374
  • 20
  • 26
Quincy Norbert
  • 431
  • 1
  • 6
  • 18
  • why dont you just use the sin / cos function? by the way, the `+ aMin` is notnecessary – user287107 May 04 '20 at 23:13
  • I was using Sin initially, but couldnt find out how to go from 1 to 0 _lerpPulse = (Mathf.Sin(Time.time * 2f) + 1f) / 2f This is what is was using and works for 0 to 1 – Quincy Norbert May 04 '20 at 23:24

1 Answers1

1

Mathf.PingPong creates a graph like this enter image description here So, to begin at a peak, you can add the distance to the peak along the x axis to the time parameter, which will be sqrt(2)/2 * the length, or 0.707 * length

float PingPong1To0(float aValue, float aMin, float aMax)
{
    float offset 0.707f *  (aMax - aMin); // sqrt(2)/2
    return Mathf.PingPong(aValue +offset, aMax - aMin) + aMin;
}

Should hit the falling side of that triangle

Jay
  • 2,553
  • 3
  • 17
  • 37
  • answer [here](https://stackoverflow.com/a/61307619/7711148) better explaining how `mathf.PingPong` works – Jay May 05 '20 at 11:38
  • This is indeed what I was looking for! The only thing I'm wondering though, is how can I change the speed of the PingPong? Initially I used Time.time * 0.5f, but this isn't working with the offset for some reason. – Quincy Norbert May 05 '20 at 13:53
  • `Mathf.PingPong((Time.time * 0.5f) + offset, aMax...`? didn't work? – Jay May 05 '20 at 16:01