- Home /
Question by
toadie-festino · Jul 08, 2015 at 09:23 AM ·
timerracing
Lap timer not updating best lap milliseconds...not efficient enough?
Hi
I have a lap timer script which currently times the current lap but also stores the players best lap and displays it to them. I store it in playerprefs. the problem I have is that if I go milliseconds faster, the best lap isn't being updated. If I go seconds faster, it is... so 00:09:90 .. if i go 00:09:50 .. it wont update.. as soon as i do a 00:08:++ it will update...
I have included the script here from one of my levels. Hopefully its fairly obvious as to what each bit does... Any ideas?
#pragma strict
var myStartTime:float;
var GameStartTime : float = 0;
var isStartTime : boolean = false;
var BestTimeText: String;
var minutes:int=0;
var seconds:int=0;
var fraction:int=0;
var timeText;
var objecttoshow : GameObject;
var newSkin : GUISkin;
function Start () {
if(PlayerPrefs.GetInt("StadiumBestTime") == null || PlayerPrefs.GetInt("StadiumBestTime") == 0){
PlayerPrefs.SetInt("StadiumBestTime",100);
PlayerPrefs.Save();
}
}
function Update () {
BestTimeText = String.Format ("{0:00}:{1:00}:{2:00}",PlayerPrefs.GetInt("Stadiumminutes"), PlayerPrefs.GetInt("Stadiumseconds"), PlayerPrefs.GetInt("Stadiumfraction") );
}
function OnGUI() {
GUI.skin = newSkin;
GUI.Label (new Rect(Screen.width/2-Screen.width/4,Screen.height/3,Screen.width/2,Screen.width/12),"Best Time: "+BestTimeText);
if(isStartTime)
GameStartTime =Mathf.Abs( Time.time - myStartTime );
else
GameStartTime = 0;
minutes =GameStartTime / 60f;
seconds = GameStartTime % 60f;
fraction = (GameStartTime * 100) % 100;
timeText = String.Format ("{0:00}:{1:00}:{2:00}",minutes,seconds,fraction);
GUI.Label (new Rect(Screen.width/2-Screen.width/4,0,Screen.width/2,Screen.width/12),"Time: "+timeText);
}
function OnTriggerEnter (collider : Collider) {
if(collider.tag=="TimeEnd"){
if(GameStartTime < PlayerPrefs.GetInt("StadiumBestTime")) {
PlayerPrefs.SetInt("Stadiumminutes",minutes);
PlayerPrefs.SetInt("Stadiumseconds",seconds);
PlayerPrefs.SetInt("Stadiumfraction",fraction);
PlayerPrefs.SetInt("StadiumBestTime",GameStartTime);
PlayerPrefs.Save();
}
Time.timeScale =1;
}
if(collider.tag == "start") {
myStartTime = Time.time;
isStartTime = true;
yield new WaitForSeconds(0.5);
objecttoshow.SetActive(false);
}
if(collider.tag == "mid") {
yield new WaitForSeconds(1.5);
objecttoshow.SetActive(true);
}
}
Comment
Your answer
Follow this Question
Related Questions
Race begins when Green light shows 2 Answers
How do I make a lap timer? 2 Answers
Starting timer when I press a key 1 Answer
How can i add a timer to this code 1 Answer