Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Having trouble with Mathf.MoveTowards

Discussion in 'Scripting' started by _eternal, Dec 1, 2015.

  1. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    303
    I can never get MoveTowards, Lerp, etc to work as they're supposed to.

    I'm trying to fade a sprite's alpha in and out through a coroutine in these two while loops:

    Code (csharp):
    1. while (sprite.color.a > 0)
    2. {
    3.     currentAlpha = sprite.color.a;
    4.  
    5.     sprite.color = new Color(1, 1, 1, (Mathf.MoveTowards(currentAlpha, 0f, duration * Time.deltaTime)));
    6.  
    7.     if (sprite.color.a == 0)
    8.         yield break;
    9.  
    10.     yield return new WaitForEndOfFrame();
    11. }
    12.  
    13. while (sprite.color.a < 1)
    14. {
    15.     currentAlpha = sprite.color.a;
    16.     sprite.color = new Color(1, 1, 1, (Mathf.MoveTowards(currentAlpha, 1f, duration * Time.deltaTime)));
    17.  
    18.     if (sprite.color.a == 1)
    19.         yield break;
    20.  
    21.     yield return new WaitForEndOfFrame();
    22. }
    I thought I got it working, but now the first loop is getting stuck at around 0.95f - something is resetting currentAlpha to 1 on some frames, but not all frames.

    Even stranger, if I leave it alone for about 20 seconds, it'll magically start working.

    Is this even how MoveToward is supposed to be used? Duration is 1. It seems like the problem is with the first argument being sent to MoveToward but I don't see what's causing it to get reset.
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    For Mathf.MoveTowards, the third parameter should be a velocity, rather than how long you want it to take to get there. MoveTowards is pretty simple to understand as it just moves your value towards the other value without passing it.

    For this lerping might be a simpler way to go about it, here's a method I use for something similar to this:

    Code (csharp):
    1.  
    2. IEnumerator FadeInCoroutine(float time)
    3. {
    4.     float i = 0;
    5.     float alpha = 0;
    6.  
    7.     while (i < 1)
    8.     {
    9.         i += Time.deltaTime / time;
    10.         alpha = Mathf.Lerp(0, 1.0f, i);
    11.        
    12.         // Do something with alpha!
    13.        
    14.         yield return 0;
    15.     }
    16. }
    I actually wrote a somewhat in-depth tutorial about Lerp, since it seems lots of people (myself included) have trouble with it starting out.
     
    _eternal and DMSolace like this.
  3. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    303
    Thanks. To clarify, what does velocity mean in a non-physics context? Is it just the rate of movement per frame (i.e Mathf.MoveTowards(currentValue, 1f, 0.1f) should take 10 frames to finish)?

    If so, does that mean Mathf.MoveTowards(currentValue, 1f, 1f * Time.deltaTime) would take 1 second to finish (regardless of the number of frames)?
     
  4. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Correct.
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723