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 TheDefibulator · Apr 12, 2013 at 07:37 PM · timerracing

How do I make a lap timer?

I'm making a racing game for a school project and I need to have a timer that counts upward from start until the player crosses the finish line. I also need the timer to go back a certain increment when the player hits an object (i.e. +Time) and then go forward when the player collides with an object like a barricade. Please help!

I figured out the timer code:

 private var timerText:GUIText;
 
 private var startTime:float = 0         
     
 function Update () {
     
     timerText = gameObject.GetComponent(GUIText);
     
     timeTaken = startTime + Time.time;
     
     timerText.text = FormatTime (timeTaken);
     
     }           
     
 function FormatTime (time : float){
     
     var minutes : int = Mathf.Floor(time / 60.0);
     
     var seconds : int = Mathf.Floor(time - minutes * 60.0);
     
     var milliseconds = time - Mathf.Floor(time);
     
     milliseconds = Mathf.Floor(milliseconds * 1000.0);
     
     var sMinutes = "00" + minutes.ToString();
     
     sMinutes = sMinutes.Substring(sMinutes.Length-2);
     
     var sSeconds = "00" + seconds.ToString();
     
     sSeconds = sSeconds.Substring(sSeconds.Length-2);
     
     var sMilliseconds = "000" + milliseconds.ToString();
     
     sMilliseconds = sMilliseconds.Substring(sMilliseconds.Length-3);
             
     timeText = sMinutes + ":" + sSeconds + ":" + sMilliseconds;
         
     return timeText;
     
         }

Now I just have to figure out how to add or subtract time depending on which object the player collides with. Thanks

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

Answer by fafase · Apr 12, 2013 at 07:41 PM

If you have not started running or finished the race is false, no counting. The rest of the time timer is increased at a rate of 1 per second. If you enter a trigger that is a counter up then you decrease the counter.

 var timer:float;
 var running:boolean = false;
 function Update(){
    if(!running)return;
    timer += Time.deltaTime;
 }
 
 function OnTriggerEnter(col:Collider){
    if(col.gameObject.CompareTag("CounterUp"))timer -= value;
    else if (col.gameObject.CompareTag("EndRace")) running = false;
 }
Comment
Add comment · Show 19 · 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 TheDefibulator · Apr 12, 2013 at 07:56 PM 0
Share

So, this would be a new script attached to my game objects that the racer could collide with?

avatar image fafase · Apr 12, 2013 at 07:59 PM 0
Share

That could be using the GUI you have on the example. Only the logic is somehow modified. You would have to use some trigger boxes that represent checkpoint and a final trigger for the end race. And this is attached to the racing object not the boxes

avatar image TheDefibulator · Apr 12, 2013 at 08:18 PM 0
Share
 private var timerText:GUIText;
 
     private var startTime:float = 0;
     
     var timer:float;
 
     var running:boolean = false;
 
      
 
     function Update () {
 
         timerText = gameObject.GetComponent(GUIText);
 
         timeTaken = startTime + Time.time;
 
         timerText.text = FormatTime (timeTaken);
         
         if(!running)return;
         
         timer += Time.deltaTime;
 
     }  
 
      
 
     function FormatTime (time : float){
 
         var $$anonymous$$utes : int = $$anonymous$$athf.Floor(time / 60.0);
 
         var seconds : int = $$anonymous$$athf.Floor(time - $$anonymous$$utes * 60.0);
 
         var milliseconds = time - $$anonymous$$athf.Floor(time);
 
         milliseconds = $$anonymous$$athf.Floor(milliseconds * 1000.0);
 
         
 
         var s$$anonymous$$inutes = "00" + $$anonymous$$utes.ToString();
 
         s$$anonymous$$inutes = s$$anonymous$$inutes.Substring(s$$anonymous$$inutes.Length-2);
 
         var sSeconds = "00" + seconds.ToString();
 
         sSeconds = sSeconds.Substring(sSeconds.Length-2);
 
         var s$$anonymous$$illiseconds = "000" + milliseconds.ToString();
 
         s$$anonymous$$illiseconds = s$$anonymous$$illiseconds.Substring(s$$anonymous$$illiseconds.Length-3);
 
         
 
         timeText = s$$anonymous$$inutes + ":" + sSeconds + ":" + s$$anonymous$$illiseconds;
 
         return timeText;
 
     }
     
     function OnTriggerEnter(col:Collider){
     
    if(col.gameObject.CompareTag("CounterUp"))timer -= 10;
    
    else if (col.gameObject.CompareTag("EndRace")) running = false;
 }

I added your suggestion to the current code. Then I added it to the "racing object" this would be my car. Then I got an error saying that "object reference not set to an instance of an object" What does this mean? Sorry, I am very new to this.

avatar image fafase · Apr 12, 2013 at 08:25 PM 0
Share

Where is it happening?

avatar image TheDefibulator · Apr 12, 2013 at 10:01 PM 0
Share

It happens on startup.

Show more comments
avatar image
0

Answer by TheDefibulator · Apr 13, 2013 at 03:33 AM

Any other suggestions?

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

12 People are following this question.

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

Related Questions

How to set lap timer for network multiplayer 0 Answers

How can I make a "Fastest Lap" scoring system for a racing game? 2 Answers

Lap timer not updating best lap milliseconds...not efficient enough? 0 Answers

Starting timer when I press a key 1 Answer

Race begins when Green light shows 2 Answers


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