- Home /
Counting large numbers in 1 second by increment by +1
At the moment my game will just +50 to current store in one jump in a second, but I want it to increment that +1 at a time, so it would be 1/50ths of a second increments. I understand this this code.
But what happens if you are getting by 100,000 per second? or 1,000,000 per second or more! While still having counters that increment at +1 (Or appear to as you cant see the numbers they are just a blur) is there some type of trickery going on here that I've seen other games use?
Answer by CreepyCat2 · Jun 22, 2019 at 12:04 AM
@CrispyDev How I would go with this is using booleans and Coroutines heres a example
public int Number = 0;
bool hasPassed = false;
public void Update()
{
if (hasPassed == false)
{
Number++;
StartCoroutine(Prevent());
}
}
IEnumerator Prevent()
{
hasPassed = true;
yield return new WaitForSeconds(1f);
hasPassed = false;
}
if you want to customize how fast it goes add this
public float time = 1f;
and change yield return new WaitForSeconds(1f);
with
yield return new WaitForSeconds(time);
Yes this is a pretty messy way of doing it, but this is how i did it, and it worked.
Answer by Kennai · Jun 22, 2019 at 08:43 PM
Oh its old topic lol but okay, better late than never
How about this? I used float variables for precise calculations. And there is integer variable (if needed) to store current amount as whole numbers.
public float desiredAmount = 20000f;
public float currentAmount = 0f;
public float amountPerSec = 1000f;
public int curIntAmount;
public void Update()
{
if(desiredAmount > currentAmount)
{
currentAmount += amountPerSec * Time.deltaTime;
if(currentAmount > desiredAmount)
currentAmount = desiredAmount;
curIntAmount = (int) currentAmount;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
how to Save A random generated number and use it 1 Answer
How to fix my Life counter 1 Answer