Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pmadden · Feb 06, 2012 at 10:24 PM · javascripttimertriggersrace

Javascript Basic Lap Timer with GUI

I have been searching the forums for literally days, and can't find a working answer. I have tried dozens of possible solutions but nothing is working for me. Basically, I have a basic racing game. I have a nice little welcome screen, with a GUI that loads the map with the track. I got a countdown timer to say 3, 2, 1, GO! But now I'm trying to figure out how to make a lap timer. I only need the car to do one lap, and then it uploads the timer to a server I have. I have tried using triggers to start and stop the time, but nothing seems to work. I would post code, but there are just so many versions I have tried. If anyone knows of any good scripts in the wiki, or tutorials on the internet, literally anything, it would be greatly appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by POLYGAMe · Feb 06, 2012 at 11:33 PM

You're welcome to use mine. It's a little more complex and probably a little rough as I'm a bit of a noob but it does the job. It takes into account multiple laps and displays the last lap etc etc and can also detect if the lap was legitimate (though that needs tweaking). Take from it what you want ;-)

 // Public Variables
 var ValidLap : GameObject;
 var LapCounter : GameObject;
 var LapCounterText : GUIText;
 var LapCounterTextShadow : GUIText;
 var iStartSequenceTime : int = 9;
 var LapTimeText : GUIText;
 var LapTimeTextShadow : GUIText;
 var LastLapText : GUIText;        // Displays the previous lap time in the centre of the screen for the player
 var LastLapTextShadow  : GUIText;
 var iNumberOfLaps : int = 5;
 var fAllLapTimes : float[] = new float[iNumberOfLaps]; // An array to store all laptimes.
 
 
 // Private Variables
 private var bValidLapIsTrue = false;
 private var iCurrentLap : int = 1;
 private var iMinutes : int;
 private var iSeconds : int;
 private var iHundredths : int;
 private var iMinutes2 : int;
 private var iSeconds2 : int;
 private var iHundredths2 : int;
 private var fCurrentLapTime : float;
 private var fPreviousLap : float;        // Float to store the previous completed lap
 private var fLapTimeTotal : float;        // Container to store all previous laptimes together.
 private var bSetLapTime = false;
 private var bCurrentLapReset = false;
 
 
 
 function OnTriggerEnter(other : Collider)
 {
     if(other == ValidLap.collider)
     {
         if(bValidLapIsTrue)
         {
             bValidLapIsTrue = false;
         }
         else
         {
             bValidLapIsTrue = true;
         }
     }    
     
     if(other == LapCounter.collider && bValidLapIsTrue)
     {
         ++iCurrentLap;
         bValidLapIsTrue = false;
         bSetLapTime = true;
     }    
 }
 
 
 function Update()
 {
     // Update the onscreen lap counter
     LapCounterText.text = ("LAP " + (iCurrentLap));
     LapCounterTextShadow.text = ("LAP " + (iCurrentLap));
     
     if (LevelSetup.g_bGameInPlay)
     {
         if (!bCurrentLapReset)
         {
             fCurrentLapTime = Time.time - iStartSequenceTime; // Account for the starting sequence delay
         }
         else
         {
             // Reset the current lap timer, accounting for original start seq delay
             fCurrentLapTime = ((Time.time - fLapTimeTotal) - iStartSequenceTime);
         }
     }
     else
     {
         // Keeps timer at zero until game underway
         fCurrentLapTime = 0.0f;
     }
     
     // Lap timer
     iMinutes = (fCurrentLapTime /60f);
     iSeconds = (fCurrentLapTime  % 60f);
     iHundredths = ((fCurrentLapTime * 10) %10);
     LapTimeText.text = String.Format("{0:00}:{1:00}:{2:00}",iMinutes,iSeconds,iHundredths);
     LapTimeTextShadow.text = String.Format("{0:00}:{1:00}:{2:00}",iMinutes,iSeconds,iHundredths);
     
 
     // Set the previous lap time
     while (bSetLapTime)
     {
         fPreviousLap = fCurrentLapTime;
         print(fPreviousLap);
         bSetLapTime = false;
         fCurrentLapTime = 0.0f; // Reset the current Laptime
         bCurrentLapReset = true;
         fLapTimeTotal += fPreviousLap; // Adds to the total race time
         DisplayLapTime();
         
         if (iCurrentLap <= fAllLapTimes.Length) // So array doesn't go out of bounds
         {
             for (var i = 0; i < iCurrentLap; ++i)
             {
                 fAllLapTimes[i] = fPreviousLap; // Populate the array with lap times
                 print("Lap: " + fAllLapTimes[i]);
             }
         }
     }
 }
 
 
 function DisplayLapTime()
 {
     var LastLapTextCopy = Instantiate(LastLapText);
     var LastLapTextShadowCopy = Instantiate(LastLapTextShadow);
     
     iMinutes2 = (fPreviousLap /60f);
     iSeconds2 = (fPreviousLap  % 60f);
     iHundredths2 = ((fPreviousLap * 10) %10);
     
     for (var i = 0; i < 5; ++i)
     {
         LastLapTextCopy.text = ("LAST LAP: \n" + String.Format("{0:00}:{1:00}:{2:00}",iMinutes2,iSeconds2,iHundredths2));
         LastLapTextShadowCopy.text = ("LAST LAP: \n" + String.Format("{0:00}:{1:00}:{2:00}",iMinutes2,iSeconds2,iHundredths2));
     
         yield WaitForSeconds(0.5);
         
         LastLapTextCopy.text = (" ");
         LastLapTextShadowCopy.text = (" ");
         yield WaitForSeconds(0.5);
     }
     
     
     yield WaitForSeconds(3.0f);
     Destroy(LastLapTextCopy);
     Destroy(LastLapTextShadowCopy);
 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pmadden · Feb 07, 2012 at 01:17 AM 0
Share

Thanks! I'm having a bit of trouble implementing this.. can you explain how you have it setup in your game? Its giving me a compiler error "(61,9): BCE0005: $$anonymous$$ identifier: 'LevelSetup'. " The code says: if (LevelSetup.g_bGameInPlay)

I appreciate your help!

avatar image POLYGAMe · Feb 07, 2012 at 02:07 AM 0
Share

Ah, sorry, that part of the script is referencing another script (LevelSetup is a script and bGameInPlay is a bool in that script). You should be able to read through this though and take the bits you need... it's reasonably well commented ;-)

avatar image pmadden · Feb 07, 2012 at 02:10 AM 0
Share

Ok Thanks. I suppose that part should just be removed altogether.

also, when I try to assign the GUI Texts using the editor, it returns errors. Ideas?

avatar image POLYGAMe · Feb 07, 2012 at 02:36 AM 0
Share

What are the errors? Have you created the GUIText gameobjects and linked to them?

avatar image pmadden · Feb 07, 2012 at 02:47 AM 0
Share

Im not near my computer now, but it gave me some error along the lines of "LapCountText is not a defined variable" or something like thay. I will let you know the exact message when I get home... Thanks again

Show more comments
avatar image
0

Answer by Kryptos · Feb 06, 2012 at 11:02 PM

At the beginning of the race (just after the countdown):

 this.startTime = Time.time; // startTime is a float member of your script

At the end:

 float fTime = Time.time - startTime; // absolute value in seconds
 int nHundredths = (int)(Mathf.Floor(fTime*100))%100;
 int nSecond = (int)Mathf.Floor(fTime);
 int nMin = (int) nSecond/60;
 nSecond = nSecond%60;
 string sText = string.Format("{0:00}:{1:00}.{2:00}", nMin, nSecond, nHundredths);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Timer Between Labels 2 Answers

Need some help an object collider that stops a timer then displays it on another scene 1 Answer

Why my button in for loop isn't working? 1 Answer

Race Start Triggers 1 Answer

Check which waypoint is closer 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges