- Home /
A Split Second Timer;
Hey Guys and Girls,
I am trying desperately now to make a split second timer, I can't seem to get the equation right for them, this is what I have developed so far.
var timerSec = Mathf.Round(timer);
var splitSeconds : int = (timerSec * 100) % 100;
alertTimer = String.Format ("{0:00}:{0:00}", timerSec, splitSeconds);
This is meant to count down from 20, the "seconds" part of it works fine, but I can't make split seconds, it keeps giving me the same as "seconds".
I hope that 1) you know I mean and 2) someone can point me in the right direction.
Thanks in advance!
Answer by KellyThomas · Jan 15, 2014 at 03:15 PM
You are using your already rounded value to calculate splitSeconds.
What you want is the difference between timer and timerSec.
Assuming timer is a float measuring seconds this should work:
var fractions: int = 100; // for hundredths of sec
var timerSec: int = Mathf.RoundToInt(timer);
var splitSeconds: int = Mathf.RoundToInt((timer - timerSec) * fractions);
alertTimer = String.Format ("{0:00}:{0:00}", timerSec, splitSeconds);
Answer by JoaoOliveira · Jan 15, 2014 at 03:15 PM
You are using the round value when calculating the split seconds. Try changing to this:
var splitSeconds : int = (timer * 100) % 100;
Also, I think that doesn't work the way you want with floats, so maybe you need this:
var splitSeconds : int = ((int)(timer * 100)) % 100;
Answer by HappyMoo · Jan 15, 2014 at 10:57 PM
"{0:00}:{1:00}"
Also, your splitseconds should use timer, not timerSec, as timerSec is an Int already.
var timerSec = $$anonymous$$athf.Round(timer);
var splitSeconds : int = $$anonymous$$athf.Round(timer * 100) % 100;
alertTimer = String.Format ("{0:00}:{1:00}", timerSec, splitSeconds);
Answer by rutter · Jan 15, 2014 at 11:28 PM
If you want to track fractional seconds, why are you using an int?
var secondsLeft : float;
function Start() {
secondsLeft = 20.0;
}
function Update() {
secondsLeft -= Time.deltaTime;
}
function OnGUI() {
GUILayout.Label(secondsLeft.ToString("n2"));
}
Your answer