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 Alex 9 · Apr 19, 2011 at 04:14 PM · inputtimestartstopuser

Stop Watch start when told

I am making a game in which case there is a stop watch that keeps on counting up (in seconds) when the player hits the "E" key. But I am having trouble. Depending on how long the player takes, it will start the stop watch at however long he takes.

Example: If the player takes 10 seconds before pressing E to start the timer, the timer will start at 10 and continue to count up from there

Here is what my code looks like:

var survivalTime = 0; var subsurtimel = 0; var starttime; var didStartTime = false; var acsurvivaltime = 0;

function Start(){
subsurtimel = Time.time;
}

function OnGUI(){
GUI.Label(Rect( 250,150, 200, 30), "Survival Time: " + acsurvivaltime);
}
function Update () {
acsurvivaltime = subsurtimel;
if(PlayRocketAnimation.canlaunch){ //if the user hits the E key

 if(!didStartTime){  
   survivalTime = (Time.time - subsurtimel) + 1 ;

 }

} }

Comment
Add comment · Show 1
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 Justin Warner · Apr 19, 2011 at 04:28 PM 1
Share

Please format and code your questions please by highlighting the code and pushing the 10101 button up top. Thanks!

2 Replies

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

Answer by Justin Warner · Apr 19, 2011 at 04:31 PM

Alright, to get input: http://unity3d.com/support/documentation/ScriptReference/Input.html

To get the time in seconds: http://unity3d.com/support/documentation/ScriptReference/Time.html

So, get the start time, get the time they push the button, get the difference between, and then the add that to the time they push the button, and thats the new time they have for the timer to go up...

So put the Input in the update function, and then use the times as I explained above. Pretty easy...

Is their a specific question you have? You don't really specify one.

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
avatar image
0
Best Answer

Answer by Statement · Apr 19, 2011 at 04:41 PM

How about just incrementing a timer from the moment the user pressed E?

var timer : float = 0;

// Wait until E is pressed, and canlaunch is true. while (!(PlayRocketAnimation.canlaunch && Input.GetKeyDown(KeyCode.E))) { yield; }

// Then go into a loop where you increase the timer every frame. // Alternatively you can set some condition when to stop the timer. while (true) { timer += Time.deltaTime; yield; }

function OnGUI() {
var msg = String.Format("Survival Time: {0}", Mathf.FloorToInt(timer)); GUI.Label(Rect(250,150, 200, 30), msg);
}

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

No one has followed this question yet.

Related Questions

Looking for a script to call function if there is no user input for set amount of time. 1 Answer

Text Prompt Problem 1 Answer

How to add Hit Stop Frames? 0 Answers

Timer that stops at the end of the game 3 Answers

How can I use multiple GetButtonDown keys simultaneously? 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