Search code examples
c#unity-game-enginelerpmath-functions

Mathf.PingPong from 1 to 0


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!


Solution

  • 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