Cooldowns with Time.time

Hey guys, usually I’m fairly decent with debugging but this seems fairly hard to google. I am basically trying to check if time < time + cooldown time however using a float of 0.3 (which seems like .3 of a second? or something low) it always seems to return true even after waiting for a long time?

Should I be using something besides float? I tried to make an actual Time class variable but obviously that didn’t work out for me. Code below.

using UnityEngine;
using System.Collections;

public class Stats : MonoBehaviour {

public int HP = 100;
public int score = 0;
public int currentCombo = 0;
public int bestCombo = 0;
public float lastKill = 0f;
public float comboTime = .3f;

void Combo (){

	if (Time.time < Time.time + comboTime) {
					currentCombo ++;
	} 
	else {//if (Time.time > Time.time + comboTime) {
		if (bestCombo < currentCombo) {
			bestCombo = currentCombo;
		}
		currentCombo = 0;
	}

}

}

FYI Combo is called by sendMessage on each enemy death which seems to be working fine and the else if is commented out because I was trying to get ANYTHING else to return. Thanks!!!

Your if(Time.time < Time.time + comboTime) will always be true since you are checking if time is less than time + comboTime.

This is something like:

if(1 < 1 + 0.3) 

which is always going to be true.

If you are trying to check if time is less than cooldown time then it should be:

if(Time.time < startTime + comboTime)

Here, startTime is the time when your cooldown started. So when your condition occurs you assign your Time.time to your startTime.

so,

startTime = Time.time;

now check if the time passed since your condition occurred is less than your startTime+comboTime, which means your cooldown time is still not ended.

if(Time.time < startTime + comboTime)

your if statement will always happen because you are checking Time.time is less than Time.time + 0.3 seconds

if time.time is 1 then its if(1 < 1 + 0.3)

which you can see will always happen. what i’m thinking you need to do is take the time from when the last hit happened + combotime

if(Time.time < lastAttackTime + comboTime)
{
    currentCombo++;
    lastAttackTime = Time.time;
}

got no clue if that actually works but might help you :slight_smile: