- Home /
Duplicate Question
How to make a time based game?
Hi,
I have made a game but I want to make it time-based and I don't know how to do that.
Example: Game is supposed to be 10 minutes long and as the player progresses through the game, the time should decrease until it reaches 0 (which means game over).
Also, my game requires to pick up certain items which increase the time for the player. Example: The time to complete the game is 10 mins. When the player is down to the 5th minute, he picks up a pencil. This items increases his time to 7 mins. How do I do that?
Please help. Thanks a lot.
use Invoke() or InvokeRepeating() for simple timers in Unity. this is the most asked question on the site. search for 10,000s of questions on timers, invoke, yield, time.deltatime etc etc. unityGE$$anonymous$$S.com for many good beginner articles on it
Answer by Fornoreason1000 · Apr 30, 2013 at 10:33 AM
Time.DeltaTime is your friend here. just make a timer that decreases by Time.DeltaTime that is making a var that starts at 10(minutes) or 600(seconds) then decreases it each frame in Update().
in the timer script just make a function that adds to the timer, this will get called when the player revives and item.
Displaying the time as Min:Sec IS little more tricky, but still easy in the grand scheme of things. ToMinutes
float minutes = Mathf.Floor(timer / 60).ToString("00");
float seconds = (timer % 60).ToString("00);
when the timer reaches zero use a Quit Function or a game over function. now there other questions like this. http://answers.unity3d.com/questions/45676/making-a-timer-0000-minutes-and-seconds.html
http://answers.unity3d.com/questions/25614/how-do-i-format-a-string-into-daysminuteshoursseco.html
http://answers.unity3d.com/questions/200733/timetime-as-minutes-and-seconds-.html
and about 20 others in the Related Questions
Answer by HannesR · Apr 30, 2013 at 10:33 AM
Have a variable called timeLeft set to 600 (for 10 mins). In Update just subtract Time.deltaTime from it. To add time, just add the amount of seconds to it.