- Home /
InvokeRepeating gets crazy when Time.timeScale is increased
I have a simple 2D game in Unity 2017.2 where the player has to jump obstacles, and to raise difficulty I want to increase the speed of the game, so i am using a InvokeRepeating method to increase Time.timeScale peridiocally, but when I do that, the 1st time the InvokeRepeating method starts right, but after modifiying the timeScale, the following times that the the InvokeRepeating method is executed it goes much faster in each execution. It is like it is not interpreting that 1 second is actually 1 second anymore, because the timeScale has been modify. I hope I explain myself right. This is the code simplyfied: `
void Update() {
InvokeRepeating("GameTimeScale", 6f, 6f);
}
void GameTimeScale(){
Time.timeScale += 0.25f;
Debug.Log ("Ritmo incremental. TimeScale: " + Time.timeScale.ToString ());
}
Any idea? I did something wrong? I am kind of new with unity
Answer by Buckslice · Dec 02, 2017 at 04:52 AM
You are correct, InvokeRepeating is based of Time.timeScale so your function is affecting itself. You can simply recreate InvokeRepeating's functionality using Coroutines, this way you can specify you want the timing to be based off the real time since startup.
Coroutine increaseRoutine;
public void Start() {
increaseRoutine = StartCoroutine(IncreaseGameTimeScale(6f, 6f, 0.25f));
// call this if you want to stop increasing at some point
//StopCoroutine(increaseRoutine);
}
IEnumerator IncreaseGameTimeScale(float delay, float interval, float increaseAmount) {
yield return new WaitForSecondsRealtime(delay);
while (true) {
Time.timeScale += increaseAmount;
yield return new WaitForSecondsRealtime(interval);
}
}
Thank you so much.
It works perfectly.
I thought i could use InvokeRepeating somehow.