How can I check how long the player has been playing for in Unity?
1 Answer
To know how long the game has been running in wall time as of the start of the current frame, you can use Time.unscaledTime
. As described in the documentation, this gives:
The timeScale-independent time for this frame (Read Only). This is the time in seconds since the start of the game.
Because this is unaffected by timeScale
, it won't distort your real-world time measurement if you have slow-mo effects in your game, or if performance problems cause your game simulation to lag. It will also count time spent while the game is paused with timeScale = 0
, if that's something used in your game.
To check whether 30 seconds have passed, you can write:
bool _thirtySecondsPassed = false;
void Update() {
if (_thirtySecondsPassed == false && Time.unscaledTime >= 30f) {
Debug.Log("Thirty seconds have passed.");
_thirtySecondsPassed = true;
}
}
Here the boolean member variable ensures you print the message just once, not every future frame after the initial 30 seconds are up.
-
\$\begingroup\$ how about this: how can i make the number 700,000 divided by the seconds the player has been playing for and then add that to the score (it shouldn't have a decimal point). Does that make sense?? this is what I have:
if(Time.time > 30f) { userInput.instance.score += (int)(700000 / Time.time); }
. But that doesn't take the seconds and divide the number. \$\endgroup\$– DavidA_Nov 5, 2021 at 13:24 -
\$\begingroup\$ You probably don't want
+=
here. Otherwise you'll get 700k/30 = 23 333 on the first frame after the 30 second mark, then add 700k/30.033 = 23 307 on the very next frame for a total of 46 640, etc, stacking up faster on machines that run at a higher frame rate. You might want=
instead. Questions about how to do arithmetic in C# are general programming - there's nothing about division and addition that's unique to game development. You can search our general programming sister site StackOverflow for help with that. \$\endgroup\$– DMGregory ♦Nov 5, 2021 at 13:39 -
\$\begingroup\$ no because I'm running the if statement once the game is finished and theres bonus points added to the score. And the bonus points are calculated by dividing 700000 by the number of seconds played. This is what I want to do. And my question is : how can I divide 700000 by the number of seconds played. \$\endgroup\$– DavidA_Nov 5, 2021 at 14:33
-
\$\begingroup\$
value = numerator / denominator
- there's no secret trick here.. The example above uses the time since the game executable started up. If you want to measure the time in the current level/run, be sure to subtract a time stamp you saved at the start of the level / run. \$\endgroup\$– DMGregory ♦Nov 5, 2021 at 14:49 -
\$\begingroup\$ ok. I feel like it would be easier if you would just give me the code. I got no idea what value = numerator / denominator means. And I got no idea what secret trick you think i think there is. Sorry mate, but some people are really dumb for example me. I could easily research it myself if only i had the code \$\endgroup\$– DavidA_Nov 5, 2021 at 15:14
Time
class, no? What do you need that's not adequately explained in the API documentation? \$\endgroup\$if (Time.time >= 30.0f)
? Generally for floating point numbers, you want to compare ranges, not exact equality, since being off by 0.00001 is more than enough for exact equality to fail \$\endgroup\$