Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What Is The Shortest WaitForSeconds Time Possible, Except 0?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
}

>Solution :

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.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading