Endless Runner Speed Increase - Help With Timer

Hi,

I have the following function, which lets me increase my player Speed by an amount I supply after waiting a amount of time I supply. This is working fine, however, as soon as the game starts, it automatically adds the first increment, I think I can see what is wrong with my code, but unsure as to how best to fix it, any pointers, I’d like it to wait x Seconds before the first change, instead of instantly ?

// Increase Player Speed time check
    if(speedIncreaseTimer < Time.time) {
        speedIncreaseTimer = Time.time + speedUpWait;

        increasePlayerSpeed();
    }
}

private void increasePlayerSpeed() {
    if (playerSpeed == maxPlayerSpeed)
        return;
    playerSpeed += speedIncrement;
}

Try this:

// Increase Player Speed time check
    if(speedIncreaseTimer < Time.time - 1 ) {
        speedIncreaseTimer = Time.time -1 + speedUpWait;
        increasePlayerSpeed();
    }
}
private void increasePlayerSpeed() {
    if (playerSpeed == maxPlayerSpeed)
        return;
    playerSpeed += speedIncrement;
}

it will wait 1sec till the first increase just use the value you want.

1 Like

Thanks, just what I needed. :sunglasses:

Why not just initialize in start to this
speedIncreaseTimer = Time.time + speedUpWait;
My guess you are setting it to 0? And checking in update?

If you have really slow startup times, this could cause problems too.

yes that would be the better solution. But he didn’t show the part where speedIncreaseTimer is declared. Maybe it’s already set to something else.

Hey, @daxiongmao

That’s exactly what I ended up doing with my code, yes, it was set to 0 and I was checking in update.