- Home /
Question by
InfamousStudios · Jan 10, 2013 at 06:20 PM ·
objecttime.deltatimetimescalecounter
Displaying Time left
How would I display a time counter on the screen? What I mean is, I want a time limit to a level, But I don't want the player running around without knowing how much time it has left. I would like a time counter to display on the screen that counts down how many minutes and seconds the player has left before they run out of time. Any suggestions?
Comment
Answer by Griffo · Jan 23, 2013 at 11:32 AM
In Javascript, enter how many seconds you want in the inspector.
#pragma strict
var counter : float;
private var guiTime : float;
function Awake(){
guiTime = counter;
}
function Update(){
if(guiTime < 0){
guiTime = counter;
}
if (Input.GetKey ("f")){
guiTime += Time.deltaTime;
}else{
guiTime -= Time.deltaTime;
}
}
function OnGUI(){
if (guiTime > -1){
var hours : int = guiTime / 3600;
var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;
var text = String.Format ("{0:00}:{1:00}:{2:00}:{3:00}", hours, minutes, seconds, fraction);
GUI.Label (Rect (10, 10, 100, 30), text);
}
}