Wednesday, 24 March 2010

Timer

Next stage of the HUD to do... is the timer. Basically, I am implementing a timer in the format minutes:seconds:milliseconds which tells the user how long they have been racing (and also to work out methods such as lap time).

I found another nice tutorial at:

http://answers.unity3d.com/questions/4009/how-to-make-a-timer-in-the-game

It basically uses the Time function built into unity to get a reading in minutes, seconds etc. The code is written for the OnGUI() method so I needed to convert in so that it runs in the Update method (so that I can pass the timer readings to a GUIText).

The final outcome is below, but there are some problems with the timer at the moment. For instance, the minutes increase when seconds = 30 and not 60. I triend to fix this by doubling the reading of the minutes, but this results in the minutes increasing by 1 after the first 60 seconds, and then again every 120 seconds after.

private var startTime;
var textTime : GUIText;

function Awake() {

startTime = Time.time;

}

function Update() {
var guiTime = Time.time - startTime;

var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;

textTime.text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
If I find a fix for this then expect an update.

No comments:

Post a Comment