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 Gdayy · Nov 19, 2013 at 04:32 AM · javascriptmovement

How to check for rapid key presses?

I've finally created a movement script I like, but now i'd like to add another little feature to it.

I'm just wondering if there was any way to make my character move at a fixed rate based on multiple key presses.

ex. I rapidly hit my forward button, W. I want the script to notice when I have pressed the key 4 times within 1 second.

Is this possible? And how would I go about doing this?

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 Jamora · Nov 19, 2013 at 06:45 AM 0
Share

It's possible. You already have your problem well defined: check if the W-key is pressed 4 times within one second.

Next you need to figure out:

  • how to check for keypresses.

  • how to deter$$anonymous$$e if 1 second has passed.

Once you know how to achieve both of these, counting the presses within that second is a simple task.

1 Reply

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

Answer by Aram-Azhari · Nov 19, 2013 at 07:07 AM

You do something like:

 ...
 
 public yourClass : Monobehavior
 {
    public float firstWpress=0;
    public int count=0;
 
    void Update()
    {
       // Check for the W key.
       if (Input.GetKeyDown(KeyCode.W))
       {
          // The current time
          var currentTime=Time.time;
          // Is this the first time W was pressed?
          if (count==0)
             {
             firstWpress=Time.time; // Save the time of first
             }
          count++; // increase the key count.
          if (count==4) // has the key been pressed 4 times?
           {
            count=0; // Reset the counter
            if (currentTime-firstWpress<1) // was it under 1 second?
            { 
              // It was pressed 4 times within a second
              // Do your logic here.
            }
           }
          if (currentTime-firstWpress>1) // Reset the counter if it's been longer that 1 second.
          {
           count=0;
          }
 
        }
 
     }
 }

I haven't tried the code, but I hope that would give you some idea.

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 Gdayy · Nov 19, 2013 at 09:48 AM 0
Share

It works, just needs a little refining. Thanks.

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

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

Related Questions

I am trying to make a movement script for my main camera but it isnt working! 2 Answers

Slowing Down When Sliding Across Walls 2 Answers

Horizontal axis not working after animation 1 Answer

4 Direction Movement Problem 1 Answer

Move Object Smoothly 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