- Home /
Increase/Decrease Over Time, Not Frames
Hello,
Just to clear things up, I want to know an easy way to increase, or decrease a value over time (seconds) as apposed to the frame.
So normally, if I wanted to increase a value, I would just do value++ in the update, which increases the value every frame. What about every second? Is there an easy way to do that within an update?
Thanks
Answer by syclamoth · Jan 23, 2012 at 12:18 AM
There is a simple value that translates between seconds and frames. If you want to increment a floating-point number evenly over time, you can use
number += Time.deltaTime;
Over one second it will increase by one.
However, if you want to increment an int exactly once a second, you can use a coroutine:
int incrementThis = 0;
IEnumerator NumberIncrementer(ref int number)
{
while(true)
{
yield return new WaitForSeconds(1);
number++;
}
}
void Start()
{
StartCoroutine(NumberIncrementer(ref incrementThis));
}
void Update()
{
Debug.Log(incrementThis);
}
This way it will increment the number exactly once a frame.
InvokeRepeating would be simpler, and slightly more accurate and efficient.
@Eric5h5 - Can InvokeRepeating be placed in an Update?
The point of InvokeRepeating is that it's not Update, it repeats at given intervals.
InvokeRepeating needs to use string reflection to get a method name, and you can't pass paramaters into the method in question. It is a little less versatile because of this.
Your answer
Follow this Question
Related Questions
How Increase And Decrease And Reset Spawn Rate Over 0 Answers
How increase and decrease and reset spawn rate over time? 1 Answer
Decrease a value over time? 2 Answers
increase over time 1 Answer
CountDown Timer Help (Seconds problem) 2 Answers