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 k_pragya · Oct 08, 2012 at 10:19 AM · javascriptcar

Timer stopped on gameover in car racing game unity

Hello everyone,

I have made a simple car racing game using keyboard input..In my game when game is over my car is stopped but timer is still working..I want to stop the timer when car stop..but I don't want to use Time.timeScale function bcoz it stopped whole game..please help me ...i will really appreciate your time taken to preview this post..:)

thanks in advanced.. Enjoy..:) :)

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
0

Answer by fafase · Oct 08, 2012 at 10:37 AM

Enclosed your timer into an if statement:

 if(running)//timer logic
 
 if(carAtLastCheckpoint)running=false;

Considering your timer goes like this:

 var running:boolean=true;
 
 var timer:float;
 
 function Update(){
   if(running)timer += Time.deltaTime;
   if(Input.GetKeyDown(KeyCode.Space))running = !running;
   print(timer);
 }

The program above will print the time as it goes and if you press space the timer stops. Press again the timer goes again.

In your game you would have a endOfRace boolean with this below attach to the last checkpoint with a trigger box:

 function OnTriggerEnter(other:Collider){
     if(other.gameObject.tag == "Car")endOfRace = true;
 }

The little example program uses Input for you to try, what you would need instead in your game is:

 if(endOfRace){ 
    running = false;
    endOfRace=false;
  }

Any questions, go ahead as a comment.

Comment
Add comment · Show 8 · 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 k_pragya · Oct 09, 2012 at 11:24 AM 0
Share

Hi fafase,

Thanx a lot for helping me..i am very new to this technology. here by i have enclosed my script plzz give it few $$anonymous$$utes. Here i am unable to understand where i have to do startGame=false so that my timer will stop..

function GameStart() { print("Timer Game Start Called"); // Startplay.text = "3"; yield WaitForSeconds (1.0); // Startplay.text = "2"; yield WaitForSeconds (1.0); // Startplay.text = "1"; yield WaitForSeconds (1.0); // Startplay.text = "Go"; yield WaitForSeconds (1.0); // Startplay.text = ""; startGame = true; }

function TimerOn(){ if(!startGame) { startTime = Time.time; } }

var TimerStop : boolean = false; function OnGUI () {

if(startGame==true) { //wait(); var guiTime = Time.time - startTime;

var $$anonymous$$utes : int = guiTime / 60; var seconds : int = guiTime % 60; var fraction : int = (guiTime * 100) % 100;

text = String.Format ("{0:00}:{1:00}:{2:000}", $$anonymous$$utes, seconds, fraction); GUI.Label (Rect (1220, 50, 100, 30), text);

}

avatar image k_pragya · Oct 09, 2012 at 11:26 AM 0
Share

Hey Fafase,

I tried your code but in that when i presed space thn only my timer stoped but i want to stop the timer at the end of the game..

thank u so much for help..:)

Pragya

avatar image fafase · Oct 09, 2012 at 05:05 PM 0
Share

The idea is that what I amdoing pressing the space bar you do the same with your car passing the final line. You would have a trigger box collider with the function OnTriggerEnter part given in the answer.

so:

 -1 add a collider box at the finish line and tick IsTrigger.
 -2 Add the code with OnTriggerEnter to the collider
 -3 Look there http://answers.unity3d.com/questions/246211/Having-scripts-interact.html to see how to get your script interacting.
 -4 Get the collider script to find the boolean in the timer script.

Hopefully you get my idea, might not be so clear...

avatar image fafase · Oct 10, 2012 at 06:59 AM 0
Share

Simple, truncate the value by assigning the float to an integer:

 var fl:float = 2.2582;
 var in:int;

 in = fl;

 print(in);

This will print 2

avatar image k_pragya · Oct 10, 2012 at 07:34 AM 0
Share

hey i want to convert float to string..And its showing like we can't convert float to String.

Show more comments
avatar image
0

Answer by k_pragya · Oct 08, 2012 at 01:08 PM

Hi,

I used startTime= Time.time; but what should i do to stop my timer..and i did like this only but timer is not stopped on game over..thanx for revert me back..:) like below:-

if(startGame==true) { var guiTime = Time.time - startTime;

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

text = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction); GUI.Label (Rect (1220, 50, 100, 30), text);
} when i do startgame==false then also my timer is working..

Pragya

Comment
Add comment · Show 1 · 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 fafase · Oct 08, 2012 at 06:39 PM 0
Share

Ok when you say "when i do startgame==false then also my timer is working.." do you mean you are using startgame==false to revert the value? because this is a logical operation. If you want startGame to become false you need to use only 1 = like this

startGame=false;

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

10 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

Related Questions

Rotation along the x-axis to that player can turn around? 1 Answer

Racing game basic javascript question 2 Answers

Let the Car-Wheels rotating, problem 1 Answer

My car dont move 1 Answer

Flip over car after tips over? 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