Still getting my head wrapped around timers in C#. Can anyone help with basic code for a timer with a decreasing interval? I need to create the effect of an effect that is speeding up,ie happening more frequently over time.
There are two solutions in general, doing the timer in update or coroutine.
The problem with update is that if you don’t need the timer anymore the update will still be called, so it makes sense to work with coroutines instead.
private void Start() {
StartCoroutine(Timer());
}
private IEnumerator Timer() {
Debug.Log("Timer started.");
float from = 10f;
float to = 0f;
float speed = 1f;
while (from != to) { //or create bool flag
from = Mathf.MoveTowards(from, to, Time.deltaTime * speed );
Debug.Log(from.ToString());
yield return null; //wait end of the frame
}
Debug.Log("Timer finished.");
}