- Home /
Question by
WesterlyCarrot9 · Sep 13, 2013 at 12:38 AM ·
javascriptguitriggertimer
How to see minutes in the timer's box?
Greetings! So i used a simple timer script that i found on the internet and combined it with some GUI and a trigger that i need for my game. My question is how could i make it so that i see minutes and seconds in the GUI Box? Right now, when the player enters the trigger and the GUI Box spawns, i can see numbers running like hell :P Thanks!
var showGUI : boolean = false;
var myTimer : float = 20;
var player : GameObject;
function OnTriggerEnter(other:Collider){
if(other.gameObject.name == "Player"){
showGUI = true;
Debug.Log("Trigger Enter");
}
}
function OnGUI(){
if(showGUI){
if(myTimer > 0){
GUI.Box(new Rect(20,50,70,50), myTimer + "");
myTimer -= Time.deltaTime;
}
if(myTimer <= 0){
GUI.Box(new Rect(20,20,100,50), "GAME OVER");
}
}
}
Comment
Best Answer
Answer by Paparakas · Sep 13, 2013 at 01:14 AM
Try doing
float myTimer = 70;
float TimeLeftInSeconds = 0;
float Minutes = 0;
float Seconds = 0;
string message = string.Empty;
function OnGUI(){
if(showGUI){
if(myTimer > 0){
TimeLeftInSeconds = myTimer -= Time.deltaTime;
if((TimeLeftInSeconds / 60) >= 1)
{
Minutes = Mathf.Floor((TimeLeftInSeconds / 60));
Seconds = TimeLeftInSeconds - (Minutes * 60);
message = "Minutes: " + Minutes + " Seconds: " + Seconds;
}else{
Seconds = TimeLeftInSeconds;
message = " Seconds: " + Seconds;
}
GUI.Box(new Rect(20,50,70,50), message);
}
if(myTimer <= 0){
GUI.Box(new Rect(20,20,100,50), "GAME OVER");
}
}
}
If you want to round up the seconds you can do Seconds = Mathf.Round(Seconds);
Never$$anonymous$$d i found it :) Thanks for your reply anyway :) Below is what i did and it works perfectly now like i want it.
var showGUI : boolean = false;
var myTimer : float = 200;
var player : GameObject;
var $$anonymous$$utes : int;
var seconds : int;
function OnTriggerEnter(other:Collider){
if(other.gameObject.name == "Player"){
showGUI = true;
Debug.Log("Trigger Enter");
}
}
function OnGUI(){
if(showGUI){
if(myTimer > 0){
GUI.Box(new Rect(20,50,70,50), "" + $$anonymous$$utes + " : " + seconds + "");
myTimer -= Time.deltaTime;
$$anonymous$$utes = myTimer / 60;
seconds = myTimer % 60;
textTime = String.Format ("{0:00}:{1:00}", $$anonymous$$utes, seconds);
}
if(myTimer <= 0){
GUI.Box(new Rect(20,20,100,50), "GA$$anonymous$$E OVER");
}
}
}
Interesting, I never knew about the % operator. It makes things a lot easier. Guess I learned something today.