6
\$\begingroup\$

I want to smoothly go from one value to a new one (moving a slider or changing color). I tried using both functions and they all basically give the same result. What is the difference between them and which one should I use?

How I used SmoothDamp:

    // Show the HUD
private IEnumerator ShowHUD() {
    var HUDCanvasGroup = GameObject.FindWithTag("HUD").GetComponent<CanvasGroup>();
    var alphaValue = 1f;
    var velocity = 0f;
    var time = 0.30f;

    yield return new WaitForSeconds(1.5f);

    // Gradually fade in the canvas
    while (!Mathf.Approximately(HUDCanvasGroup.alpha, alphaValue)) {
        HUDCanvasGroup.alpha = Mathf.SmoothDamp(HUDCanvasGroup.alpha, alphaValue, ref velocity, time);
        yield return null;
    }

    HUDCanvasGroup.alpha = 1; // Since float is not accurate, manually set the alpha to 1 after
    HUDCanvasGroup.interactable = true;

    var blur = GameObject.FindWithTag("BlurryCamera").GetComponent<BlurOptimized>();
    blur.enabled = true;
}

How I used Lerp:

public static IEnumerator Dim() {
    while (t < 1) {
        t += Time.deltaTime / 0.4f;
        dimRend.material.color = Color.Lerp(undimColor, dimColor, t);
        yield return null;
    }
}
\$\endgroup\$
  • \$\begingroup\$ BTW you are using Color.Lerp that interpolates between color instead of values \$\endgroup\$ – Hamza Hasan Feb 7 '16 at 17:25
  • 2
    \$\begingroup\$ @HamzaHasan It's on the same principle. \$\endgroup\$ – Milen Feb 7 '16 at 17:43
10
\$\begingroup\$

The mechanical difference is that Lerp is linear, while SmoothDamp follows a sigmoid function. See How to Lerp like a pro and Smoothstep for more in-depth explanation and graphs.

In terms of which one you should use, that varies from product to product, situation to situation. SmoothDamp-style is often more pleasant to the eye, especially for slower fades in/out, or where the thing being interpolated is intended to be seen as physical. I.e. SmoothDamp motion generally looks more like the object is accelerating and decelerating, rather than suddenly starting and stopping.

The linear fade of Lerp works well for very fast fades, and is (I expect) slightly quicker to compute. So, if you're doing an enormous amount of interpolation, Lerp might better suit your needs. Again, however, it can look unnatural for things that are physical.

In short, read up on what they do under the hood, play around with both of them. You'll have to decide for yourself which looks and feels better for your app.

\$\endgroup\$
0
\$\begingroup\$

SmoothDamp suits well for situations where the value it's moving towards is dynamic, like transform values, and you want it to retain an organic sense of motion and agency. This is due to SmoothDamp factoring velocity in its algorithm.

Lerp is fine for most other cases.

\$\endgroup\$

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.