Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Weird jump in Lerp

Discussion in 'Scripting' started by qwertyuiop11110, Apr 4, 2020.

  1. qwertyuiop11110

    qwertyuiop11110

    Joined:
    Mar 16, 2020
    Posts:
    24
    Code (CSharp):
    1. public class ienum : MonoBehaviour
    2. {
    3.     private void Awake()
    4.     {
    5.         Debug.Log("DIG");
    6.  
    7.         StartCoroutine(zxc(false, 2.0f));
    8.     }
    9.  
    10.     IEnumerator zxc(bool trueOrFalse, float speed = 1.0f)
    11.     {
    12.         float targetAlpha;
    13.         float currAlpha;
    14.  
    15.         float t = 0.0f;
    16.  
    17.  
    18.         if (trueOrFalse)
    19.         {
    20.             targetAlpha = 1.0f;
    21.             currAlpha = 0.0f;
    22.         }
    23.         else
    24.         {
    25.             targetAlpha = 0.0f;
    26.             currAlpha = 1.0f;
    27.         }
    28.  
    29.  
    30.  
    31.         while (t < 1.0f)
    32.         {
    33.             Mathf.Lerp(currAlpha, targetAlpha, t);
    34.             Debug.Log(t);
    35.             t += Time.deltaTime / 10;
    36.             yield return null;
    37.         }
    38.     }
    39.  
    40. }
    Result in console is "DIG", "0", "0.002", "0.004", "0.006" and then it starts counting normally so the next result is: "0.03933333", "0.04131732" etc. until "0.9996715" where it (for expected reasons) simply stops.

    Why this weird jump? How can I make it start smooth as well?

    This is an empty project specifically created for script testing, there's no other scripts running that could interfere with this script, I've also replayed the script many times to make sure this wasn't accidental hiccup in CPU response time latency.
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    It might be that Unity is struggling for a few moments when it's starting, and this might just happen in editor. Can't think anything else.

    If you trigger your Coroutine with a keypress or some other way of delay after you have pressed delay, the update is smooth if you measure the difference between the current and previous update time.

    i.e. test like this:
    Code (CSharp):
    1. float t = 0.0f;
    2. float prevT = 0.0f;
    3.  
    4. while (t < 1.0f)
    5. {
    6.     prevT = t;
    7.     t += Time.deltaTime * 0.1f;
    8.     Mathf.Lerp(currAlpha, targetAlpha, t);
    9.     Debug.Log(t + ", difference: " + (t - prevT) );
    10.     yield return null;
    11. }
     
    qwertyuiop11110 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    I find it useful to iterate the time over the interval, and derive the alpha (fraction) to decide when to stop.

    Code (csharp):
    1. float interval = 1.0f / speed;  // assuming a speed of 2.0 means "finish in half a second"
    2.  
    3. for (float t = 0; true; t += Time.deltaTime)
    4. {
    5.   float alpha = t / interval;
    6.  
    7.   float tweenedAngle = Mathf.Lerp( startAngle, endAngle, alpha);
    8.  
    9.   // do what you want with tweenedAngle here...
    10.  
    11.  
    12.   if (alpha >= 1.0f) break;  // do this check last so you will end at endAngle cleanly.
    13. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Honestly though, just go to the asset store and get a tweening asset like iTween or DOTween or any of the other freebies. Many other people have already solved all these bugs and more. Why solve 'em again?
     
  5. qwertyuiop11110

    qwertyuiop11110

    Joined:
    Mar 16, 2020
    Posts:
    24
    You were right, after subscribing the IEnumerator to a button press, now it works smoothly. Any reasons why it would hiccup so badly at the start in an empty project?
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @qwertyuiop11110 it's not the empty project but the application getting started. And remember that Editor and a built app are not the same thing.
     
    orionsyndrome likes this.
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    It probably has to do with calling it in Awake. There is no guarantee things won't get loaded and/or initialized at approximately the same time. Try calling it in Start instead.
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Yes there are. The entire editor in the background is one giant mess of the scripts running.
    I'm also quite sure you wouldn't have this hiccup in a built project, but it's still not a good practice to use Awake for things that should normally run each frame.