Time.deltaTime timer counting slowly
We are making a simple game which is somewhat based around going quickly and we wanted a timer to tell the player how long they took to beat the game. The timer worked for a very long time but recently it started counting up slower than actual seconds and we don't know why. Everyone who has talked about making timers uses the method that we used (Time.deltaTime) and it shouldn't have any issues. If anyone knows what could be happening that would be great. public class Timer : MonoBehaviour { private float time = 0f; private float time2 = 0f; public bool alive = true; public Text timer;
private void Update()
{
if (alive) {
time += Time.deltaTime;
if (time >= 59.5) {
time2 ++;
time = 0;
}
}
if (time < 9.5)
{
timer.text = "Timer:\n" + time2 + ":0" + (int)(time+0.5f);
}
else
{
timer.text = "Timer:\n" + time2 + ":" +(int)(time + 0.5f);
}
}
}
Answer by streeetwalker · Apr 24, 2020 at 07:38 AM
@NaturalTenacity, there is a tactical coding difference between using a timer as a basis for motion or event selection, and simply needing to get an elapsed time.
It sounds like you want the latter, and the code you have is a complex way of doing it with some inaccuracies built in. There is much, much, simpler way.
Get the start time at the beginning of the game, the end time at the end, and subtract the two to get the elapsed time, which will be in seconds. Format as needed for display:
private float startTime;
private void Start( ) {
startTime = Time.time;
}
private void GameOver() {
float elapsedTime = Time.time - startTime;
string minutes = Mathf.Floor( elapsedTime / 60 ).ToString( "00" );
string seconds = Mathf.Floor( elapsedTime % 60 ).ToString( "00" );
timer.text = "Timer:\n" + minutes + ":" + seconds;
}
Or if you want the running timer displayed then dispense with the above and use
private void Update() {
if (alive) {
currentTime += Time.deltaTime;
string minutes = Mathf.Floor( currentTime / 60 ).ToString( "00" );
string seconds = Mathf.Floor( currentTime % 60 ).ToString( "00" );
timer.text = "Timer:\n" + minutes + ":" + seconds;
}
}
Thanks a lot for the reply, I replaced a lot of my code with yours because you were right, my code was definitely a complex way of doing it. Unfortunately however, the timer still counts up slow using that code (I should probably specify that by slow I mean 2-3 times as slow as normal seconds). I looked through my code and my other scripts and nothing should be affecting how fast the seconds are being calculated, unless the game is literally running 3 times slower but that doesn't seem to be the case since the game runs fine.
check the time base your project time settings.
Otherwise Time.time should be based on your system clock.
Ins$$anonymous$$d of delatTime, set the startTime where your game starts (may not be in the Start function as I noted above, but the code is the same). Then each time through your update loop, use elapsedTime = Time.time - startTime as noted above.
That is, you can move the code from the GameOver function I noted above into your update loop if you need a running display of the elapsed time.
That is what I did when I changed some of my code into yours, I'm using Time.time now but it still isn't working correctly. I also checked the projects timeScale and it is set to 1 so there shouldn't be issues there. Nothing seems to be working.
We made a temporary fix in which we are just multiplying the time by a constant that makes it accurate to real time. We might just need to end up saying screw it and not care that much, this is just a first test project that we are doing for fun. It doesn't need to be perfect I just wanted to know what we were doing wrong but it seems like we really aren't.
Answer by Bunny83 · Apr 24, 2020 at 04:03 PM
I don't see any major issues with your code besides that I would strongly recommend to rename "time" to "seconds" and "time2" to "minutes". Why using abstract names? Variable names should be as descriptive as possible.
The next thing is you should change the type of time2 from float to int. You only count the minutes in whole minutes. Using a float will just call for trouble at higher numbers.
Next your roll-over between your seconds and minutes should be done like this:
float seconds = 0;
int minutes = 0;
// [ ... ]
seconds += Time.deltaTime;
if (seconds >= 60f)
{
seconds -= 60f;
minutes++;
}
timer.text = "Timer:\n" + minutes.ToString("00") + ":" + ((int)seconds).ToString("00");
The difference is that we don't loose any fractional time components. When we reach 60 seconds we simply exchange 60 seconds into 1 minute. That way the timer should be as accurate as possible.
Floating point numbers are a bit tricky. Have a look at my precision table over here and maybe have a look at Tom Scott's video on floating point numbers
Apart from all that there's no reason why the timer in your code should run slower than normal. If anything it would be slightly faster since you skip about half a second every minute. Of course such a timer would never be in perfect sync with the realworld since even the smallest inaccuracies would accumulate over time. When you said it "runs slower", did you actually measure how much slower over what time period?
Thank you for the reply, but unfortunately the issue with the timer is not in the realm of losing half a second every $$anonymous$$ute. The timer counts up only once every 3 seconds and we don't know why exactly it is happening. We built it with Time.deltaTime() like all of the timers that we found while searching for a fix. All of the code is in there, it just counts up once per frame with Time.deltaTime() and displays it to the screen. There shouldn't be a problem and it definitely shouldn't be three times slower.
Also sorry about the variable names, I wasn't sure what type of timer I was making or how I would implement it so I didn't know what to name the variables at the time and I forgot to change them. They are changed now.
Answer by Berty_Bert · Oct 30, 2020 at 08:53 PM
try using Time.**unscaled**DeltaTime; You might've changed the timescale in some other script. Do this to ensure it doesn't mess up this timer.