Hello I am creating a 3D game and I need a timer that displays how long the user has been playing for. thanks in advance
Something like this should work. Increment the seconds and when it reaches 60 add a minute. When the minutes reach 60 add an hour, etc…
float seconds;
float minutes;
float hours;
void Update(){
seconds += Time.deltaTime;
if(seconds >= 60){
minutes += 1;
seconds = 0;
}
if(minutes >= 60){
hours += 1;
minutes = 0;
}
Debug.Log("H: " + hours + " M: " + minutes + " S: " + (int)seconds);
}
using System;
...
DateTime start;
void Start() {
start = DateTime.Now;
}
void OnGUI() {
GUI.Label (new Rect(0, 0, 200, 30), (DateTime.Now-start).ToString());
}
You can use Time.deltaTime just like this.
float timer;
void Update() {
timer += Time.deltaTime;
}
Then you could use some Lower() or Round() function to only show it in seconds as this will give you many decimals.