0
\$\begingroup\$

0.001266956

private void Slow()
    {
        if (timeElapsed < lerpDuration)
        {
            valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
            playerAnimator.SetFloat("Forward", valueToLerp);
            timeElapsed += Time.deltaTime;
        }
    }

startValue value is 1 and endValue value is 0 lerpDuration value is 3 timeElapsed value is 0 and valueToLerp value is also 0

In the end, the value of valueToLerp is 0.0002498031

I could add a reset line in the end of the Slow method :

private void Slow()
        {
            if (timeElapsed < lerpDuration)
            {
                valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                playerAnimator.SetFloat("Forward", valueToLerp);
                timeElapsed += Time.deltaTime;
            }

            playerAnimator.SetFloat("Forward", valueToLerp);
            valueToLerp = 0;
        }

This will work but only when I call the method Slow once if I tried to call the method again it will not work it will never get to 0 again.

and if I'm not using these two lines in the end then it will not get to 0 even the first time.

I want that I will be able to call Slow many time and it will slowly get to 0 each time I'm calling the method.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Is your question "Why is that if I ensure a < b than a never equals to b?" \$\endgroup\$ – Alexei Levenkov Jun 3 at 23:14
0
\$\begingroup\$

Alexei Levenkov accurately summarizes your problem:

if I ensure a < b...

We only enter the if block if timeElapsed is less than lerpDuration:

if (timeElapsed < lerpDuration)

That means that timeElapsed / lerpDuration is less than 1.

And so,

valueToLerp = Mathf.Lerp(startValue, endValue, someValueLessThanOne);

...will set valueToLerp to something that is not quite endValue.

You can avoid this problem by moving your time increment to before the Lerp, not after. That way, if timeElapsed is initially just a frame or less away from lerpDuration, timeElapsed += Time.deltaTime; will put it over the top just before the Lerp acts (and it automatically clamps the value for you, so that it will not exceed 1).

\$\endgroup\$
3
  • \$\begingroup\$ When moving the increment before the Lerp it's working fine for the first time I'm calling Slow but then when calling Slow again I see that the value of timeElapsed is 3.002375 so the slow down is not working on the next time's when calling the Slow method. lerpDuration value is still 3 as it should be but the timeElapsed value is bigger than the lerpDuration value. \$\endgroup\$ – Sharper Then Ever Jun 4 at 0:00
  • \$\begingroup\$ Did you mean to reset timeElapsed to zero the next time you want to animate this transition? \$\endgroup\$ – DMGregory Jun 4 at 0:06
  • \$\begingroup\$ Yes, you right to reset the timeElapsed to zero. I will try it. \$\endgroup\$ – Sharper Then Ever Jun 4 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.