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!

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;

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);

“{0:00}:{1:00}”

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"));
}