Help Understanding -= Time.deltaTime in this case ?

I’m currently backward engineering a script I found online, in the following snippet I’ve removed all the excess code that’s not needed to be shown here for the sake of clarity. I’m using this code to control the ‘speeding up’ ( velocity ) of my player character over time, I understand most of what the script does, essentially, increase velocity by 0.05f every 10 seconds. Can anyone better explain to me, what the else { currentLevelUpTime -= Time.deltaTime; } does / is doing ? As opposed to what ‘I think’ it’s doing ?

private int timeToLevelUp = 10;
private float currentLevelUpTime;

void Start () {
    currentLevelUpTime = timeToLevelUp;
}

void Update () {
    manageLevelUp();
}

void manageLevelUp() {
    if (currentLevelUpTime <= 0) {
        levelUp();
        currentLevelUpTime = timeToLevelUp;
    }  else {
        currentLevelUpTime -= Time.deltaTime;
    }
}

void levelUp() {
    velocity += 0.05f;
}

Its a basic countdown timer. Each frame currentLevelUpTime reduces till it gets to zero, then it resets back to 10.

Turn the inspector into debug mode and you can watch it happen as you play.

1 Like