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 HikerJB · May 23, 2013 at 03:45 PM · timerkeypresswalking

Can't seem to measure time between key presses

I'm working on a walking game where a game mat emulates the key board. I need the controller to move along as long as there is continual walking on the keys. If the user stands on the keys or leaves the game mat completely, the controller should stop. I've decided to use a timer to measure the amount of time between key presses, but, it seems to just keep increasing. Pace shouold vary between .5 - 1 second, not increase> What am I doing wrong?

 function WalkTester(){
     //pacestart = 0 ;
     
     if (Input.GetKeyDown ("t")) {
         steps += 1 ;
         pacestart = Time.time ;
         Debug.Log ( pacestart);
         
     }
 
     if (Input.GetKeyDown ("y")) {
         steps += 1 ;
         paceEnd = Time.time ;
         pace = (paceEnd - pacestart) ;
         Debug.Log ( pace );
         
     }
   
     //MoveToWaypoint();
 }
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 Tomer-Barkan · May 23, 2013 at 04:06 PM 0
Share

Add in the debug "t" or "y". $$anonymous$$ake sure they are really launched in the correct order every single time.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by zombience · May 23, 2013 at 04:15 PM

If you'd like to have pace never increase beyond 1, you'd need to put a limit on it like:

 pace = (paceEnd - paceStart);
 if(pace > 1)
     pace = 1;

Otherwise, your code as you have written will result in huge pace numbers if you hit the "t" key, but say, wait for 10 seconds before hitting the y key. You may want to write a timeout script which says that if there is too much time between the two key presses, pace returns to zero.

In fact, you may do something like always lerp pace to a target value which is being reset to zero.

something like:

 targetPace = 1/(paceEnd - paceStart); // 1 divided by subtraction will result in lower numbers for higher time value difference, therefore higher pace for less time between presses
 if(targetPace > 1)
     targetPace = 1;
 
 pace = Mathf.Lerp(pace, targetPace, .1f)
 
 targetPace = 0;

this way, if no presses are detected, your "target pace" will move back to zero. otherwise, you can increase the pace slowly as you press more rapidly

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

Answer by robertbu · May 23, 2013 at 04:23 PM

I tested your code out, and it seems to work fine. Note your pasestart is going to go up, because it is set to the current running time of the application. Comment out the Debug.Log() on line 7, and test your code. For me, the numbers were consistent with my fingers walking between the two keys.

Note if you get repeated 'y' hits without a 't' hit, the time will go up since 'pacestart' is not being set. Not sure how you want to handle it. You could reset pacestart to Time.time in the 'y' code, but then a user could repeatedly hit the 'y' key without ever hitting the 't' key. Or you could add a boolean value so that the 'y' key is ignored if not preceeded by a 't' key.

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

15 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 avatar image avatar image avatar image

Related Questions

How to make player starts moving after holding a key for certain time only once 1 Answer

Starting timer when I press a key 1 Answer

how can I make an animation play on keypress but stop on release? 2 Answers

How to realize accurate time counter (millisecond precision) 3 Answers

How can I add time to a timer on an object collision? 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