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 Orloffyeah · Mar 22, 2013 at 10:18 PM · buttonspeeddetect

Detect Frecuency/Speed of Pressing a Button

Hi, for a racing game I'm working on, I had the idea of making a game mode in which you have to press as fast as you can a button to make the car go faster and win. But how could I detect the speed in which the player is pressing a button?

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
2
Best Answer

Answer by robertbu · Mar 23, 2013 at 01:04 AM

To solve this problem, you don't need to calculate button frequency. Instead make each button press add just a bit of force/speed So if you are using a Rigidbody, you could do something like:

 if (Input.GetKeyDown(KeyCode.Space))
     rigidbody.AddForce(transform.forward * small_force * Time.deltaTime);

If you are moving your car through Transform.translate(), use a speed variable. Each time they press the button, increase the speed variable just a bit.

 if (Input.GetKeyDown(KeyCode.Space))
    speed += small_value;
 transform.Translate(transform.forward * speed * Time.deltaTime);
Comment
Add comment · Show 3 · 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 Orloffyeah · Mar 23, 2013 at 02:08 AM 0
Share

Thanks, it is working, but now I would like that the speed decrease s if you don't press the button, any ideas?

avatar image robertbu · Mar 23, 2013 at 06:00 AM 1
Share

If you are using a Rigidbody, you can do one or more of the following:

  • In the inspector, increase drag for the Rigidbody

  • Change the properties of the Physic material for both the car and the surface for the Rigidbodies.

  • Add a small amount of counter force each frame. That is:

    rigidbody.AddForce(-transform.forward very_small_force Time.deltaTime);

If you are using translation, you can multiply the speed value by a friction value just under 1.0. Something like:

speed *= 0.975;

Note that both of these would happen every frame, and the other forces would only be added/changed on the press of a key. It might take some playing to get the right balance.

avatar image Orloffyeah · Mar 23, 2013 at 05:41 PM 0
Share

Thanks, your help was very useful :D

avatar image
3

Answer by BPR · Mar 22, 2013 at 10:31 PM

Hi, try to save the current time with

 float startT =time.time;
 

then count the amout of button presses by increasing an integer for each time the buttion is pressed

 if (Input.getKeyDown(...)){
 count++;
 }

then when you want to evaluate the frequency store the current time

 float endT = time.time;

then subtract the start time from the end time and divide the number of button pushes through the result

 float frequency = count / (endT-startT);

Regards, BPR

Comment
Add comment · Show 2 · 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 Orloffyeah · Mar 22, 2013 at 11:36 PM 0
Share

Hi, thanks for the quick response, but how could I write it fot java? Float = Var?

avatar image chrisall76 · Mar 23, 2013 at 12:15 AM 0
Share

Yes, in javascript it's automatically chosen so float = var

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

Making a button in unity iphone? 3 Answers

How to change pace by pressing different buttons once? 1 Answer

Detect Mouse Click OnGUI 1 Answer

How to change UI button animation speed (4.6 UI) 2 Answers

Press Any Key To Continue 0 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