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)
void Update()
{
if (isButtonpressed)
{
StartCoroutine(AnimateCoins());
}
}
void CalculateCoins(int i)
{
switch (i)
{
case 1: // Button 1 is pressed
coinsDifference = 0;
currentCoinsTemp = currentCoins;
t = 0;
coinsDifference = currentCoinsTemp - payment1;
currentCoins = coinsDifference;
isButtonpressed = true;
Debug.Log("BUY 1 Pressed!");
break;
}
}
IEnumerator AnimateCoins()
{
yield return new WaitForSeconds(0f);
t += 2f * Time.deltaTime;
currentCoins = (int)Mathf.Lerp(currentCoinsTemp, coinsDifference, t);
currentCoinsText.text = currentCoins.ToString();
Debug.Log(currentCoins);
if (currentCoinsText.text == coinsDifference.ToString())
{
isButtonpressed = false;
StopAllCoroutines();
}
}
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.