Home > OS >  What Is The Shortest WaitForSeconds Time Possible, Except 0?
What Is The Shortest WaitForSeconds Time Possible, Except 0?

Time:11-14

Using yield return new WaitForSeconds(waitTime);

within an IEnumerator, what is the shortest wait time other than 0? I have tried using a float number and have tried as low as 0.00001f for the waitTime, however i'm not sure if there is a limit or not?

The purpose is that I am having a player's coins added one unit at a time, so the meter is 'filling up' rather than instant. I have tried searching but I cannot seem to find the answer to what the shortest limit is for WaitForSeconds, so if anyone knows I'd greatly appreciate the answer.

Additionally, my code is as follows if anyone has any input on how I can speed up the process without making it instant, as it's just not quite quick enough and the player is having to sit for a while waiting for the coins to be added to the meter at the end of the game.

IEnumerator AddCoins()
{
    yield return new WaitForSeconds(1);

    while (userGainedCoins > 0)
    {
        if (addingSoundPlaying == false)
        {
            addingSound.Play();
            addingSoundPlaying = true;
        }

        if (userGainedCoins == 1)
        {
            addingSound.Stop();
        }

        userCoins  = 1;
        userGainedCoins -= 1;
        PlayerPrefs.SetInt("User Coins", userCoins);
        yield return new WaitForSeconds(waitTime);
    }

    addingSoundPlaying = false;
}

CodePudding user response:

I think it doesn't have a minimum time, it just waits until it reaches a certain amount before waiting. If you want, you can just add multiple coins at a time. I usually try not to mess with these kinds of things, and instead of messing with the coins, I would just mess with the value it displays. You could have a script that holds the number of coins and increases/decreases it based on the timestep. Something like this:

public int coins = 100;
public int displayedCoins = 0;

private void Update() {
    int change = Mathf.CeilToInt(300 * Time.deltaTime);
    if (displayedCoins < coins)
        displayedCoins = Math.Min(displayedCoins   change, coins);
    else
        displayedCoins = Math.Max(displayedCoins - change, coins);
}

and with this you can just set the amount and forget about it, and it works with gaining and losing coins. You can also change the change value to something like int change = Mathf.CeilToInt(Mathf.Abs(coins - displayCoins) * Time.deltaTime) to make it have an ease-out effect, or whatever you want. (I didn't test this code.)

CodePudding user response:

@JeffRSon provided this statement in the comments which I believe answers my question:

"Due to the nature of Coroutines this should depend on the actual framerate, which also denotes the shortest time possible."

  • Related