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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with Lerp inside a Coroutine

Discussion in 'Scripting' started by nineswordz, Dec 18, 2019.

  1. nineswordz

    nineswordz

    Joined:
    Sep 23, 2019
    Posts:
    12
    Hi, I'm writing a Purchase code that uses coins when a button is pressed, animating it and then stopping at the coins remaining. Actually, it works on the current script.

    For simulation, current coins = 50,000 & payment = 5,000 (both int)

    Code (CSharp):
    1.  
    2. void Update()
    3.     {
    4.         if (isButtonpressed)
    5.         {
    6.             StartCoroutine(AnimateCoins());
    7.         }
    8.     }
    9.  
    10. void CalculateCoins(int i)
    11.     {
    12.         switch (i)
    13.         {
    14.             case 1: // Button 1 is pressed
    15.                 coinsDifference = 0;
    16.                 currentCoinsTemp = currentCoins;
    17.                 t = 0;
    18.  
    19.                 coinsDifference = currentCoinsTemp - payment1;
    20.                 currentCoins = coinsDifference;
    21.                 isButtonpressed = true;
    22.  
    23.                 Debug.Log("BUY 1 Pressed!");
    24.                 break;
    25.         }
    26.     }
    27.  
    28. IEnumerator AnimateCoins()
    29.     {
    30.         yield return new WaitForSeconds(0f);
    31.  
    32.         t += 2f * Time.deltaTime;
    33.         currentCoins = (int)Mathf.Lerp(currentCoinsTemp, coinsDifference, t);
    34.         currentCoinsText.text = currentCoins.ToString();
    35.  
    36.         Debug.Log(currentCoins);
    37.  
    38.         if (currentCoinsText.text == coinsDifference.ToString())
    39.         {
    40.             isButtonpressed = false;
    41.             StopAllCoroutines();
    42.         }
    43.     }
    44.  
    But here's the problem, the lerp doesn't work when you click it in a rapid succession (i.e. double tap the button quickly) On a normal click, a 50,000 value will lerp to 45,000 (payment cost to subtract is 5,000). While on a rapid click (two times), it sometimes stops around 43,586 or something like that, and the 2nd rapid click doesn't work (supposedly if you tap 2 times quickly, coins should be 40,000. I'm just stopping the lerp with a text condition so apologies for my non-efficient code.

    Is there a way to wait for the lerp to 40,000 first? and then players can pay again? Any help and code optimization tip is appreciated. Thanks.
     
    Last edited: Dec 18, 2019
  2. nineswordz

    nineswordz

    Joined:
    Sep 23, 2019
    Posts:
    12
    Correction: "Is there a way to lerp to 45,000 first?"
     
  3. nineswordz

    nineswordz

    Joined:
    Sep 23, 2019
    Posts:
    12
    Update: Nevermind, I found a way to fix this. Thanks for viewing! (Thread solved)