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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by benhacking · Mar 27, 2012 at 06:47 PM · getkeydowngetkeyup

Checking if a key has been pressed twice in quick succession

Hi guys,

My game is 2.5d, you control one character, he can move left right and jump. The controls are WASD and Spacebar. What I'm trying to do is have it so that when the user presses DD or AA in quick succession it performs a dash (increases speed for about half a second)

So heres my current logic of the idea for D:

http://pastebin.com/igUWA8kg

thanks for any feedback

Comment
Add comment · Show 2
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 benhacking · Mar 28, 2012 at 06:24 PM 0
Share

I've updated my original post now with a more solid bit of code, the pastebin link - please have a look :D

avatar image Bunny83 · Mar 28, 2012 at 06:26 PM 0
Share

@benhacking: Use comments!!!

Your post isn't an answer. So use "add new comment"

4 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by felixpk · Aug 27, 2016 at 10:07 PM

I really noticed the same effect as @benhacking mentioned in the comments, so I played around a little bit and found out a nice little solution.

         if(Input.GetKeyDown(KeyCode.A) && firstButtonPressed) {
             if(Time.time - timeOfFirstButton < 0.5f) {
                 Debug.Log("DoubleClicked");
             } else {
                 Debug.Log("Too late");
             }
 
             reset = true;
         }
 
         if(Input.GetKeyDown(KeyCode.A) && !firstButtonPressed) {
             firstButtonPressed = true;
             timeOfFirstButton = Time.time;
         }
 
         if(reset) {
             firstButtonPressed = false;
             reset = false;
         }

It is interesting how the order of the if-statements is critical for this solution to work.

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 drvignesh · Mar 09, 2017 at 06:23 AM 0
Share

I'm surprised that no one else found this useful? I had tried many variations and this one worked best.

avatar image gigazelle · Sep 19, 2018 at 10:47 PM 0
Share

I used a slight variation of this, as triple tapping triggered it twice. This is my code:

         if(Input.GetButtonDown("Jump") && doubleTap) {
             if(Time.time - doubleTapTime < 0.4f) {
                 Debug.Log("Double-tapped");
                 doubleTapTime = 0f;
             }
             doubleTap = false;
         }
 
         if(Input.GetButtonDown("Jump") && !doubleTap) {
             doubleTap = true;
             doubleTapTime = Time.time;
         }
avatar image Chrysys gigazelle · Dec 22, 2020 at 02:05 AM 0
Share

I expanded on your solution to log a triple tap. I did a lot of searching and yours was the closest to what I was looking for. There's probably some more optimization potential here, like using a switch.

         /* Detect double and triple tap for Jump */
         if (Input.GetButtonDown("Jump"))
         {
             if (tripleTap) {
                 if ((Time.time - tripleTapTime) < tapResetDelay)
                 {
                     Debug.Log("Triple-tapped");
                     tripleTapTime = 0f;
                 }
                 tripleTap = false;
             }
             if (doubleTap && !tripleTap) {
                 if ((Time.time - doubleTapTime) < tapResetDelay)
                 {
                     Debug.Log("Double-tapped");
                     doubleTapTime = 0f;
                     tripleTap = true;
                     tripleTapTime = Time.time;
                 }
                 doubleTap = false;
             }
             if (!doubleTap && !tripleTap) {
                 doubleTap = true;
                 doubleTapTime = Time.time;
             }
         }

avatar image
1

Answer by numberkruncher · Mar 28, 2012 at 12:52 PM

I would do this in a different way, (code not tested but should work):

 public class MyBehaviour : MonoBehaviour {
 
     // Must double tap within half a second (by default)
     public float doubleTapTime = 0.5f;
     // Time to wait between dashes
     public float dashWaitTime = 2.0f;
 
     // Time that the dash button was last pressed
     private float _lastDashButtonTime;
     // Time of the last dash
     private float _lastDashTime;
     
     void Update() {
         if (isDashPossible && Input.GetKeyDown(KeyCode.D)) {
             // If second time pressed?
             if (Time.time - _lastDashButtonTime < doubleTapTime)
                 DoDoubleDash();
             _lastDashButtonTime = Time.time;
         }
     }
     
     bool isDashPossible {
         get {
             return Time.time - _lastDashTime > dashWaitTime;
         }
     }
     
     void DoDoubleDash() {
         _lastDashTime = Time.time;
         
         // blah...
     }
 
 }
Comment
Add comment · Show 6 · 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 benhacking · Mar 28, 2012 at 01:34 PM 0
Share

nice, that looks really promising, will have a go at implementing it :D thanks

avatar image benhacking · Mar 28, 2012 at 03:35 PM 0
Share

it turns out getkeyup is necessary so i'm integrating that at the moment

avatar image numberkruncher · Mar 28, 2012 at 04:16 PM 0
Share

Cool! getkeyup would give a slightly slower response time for the player than getkeydown but obviously that depends upon what your trying to achieve

avatar image benhacking · Mar 28, 2012 at 04:22 PM 0
Share

the reason it is necessary is that if im holding D down, each update it thinks i've pressed it again, this is bad because i don't want the user to be able to HOLD D then release and quickly press D once to claim the double tap, they must press the first D and release it just as fast (or there abouts) as the second D.

For this reason i need to check the time between keydown and keyup on the first D

avatar image numberkruncher · Mar 28, 2012 at 06:41 PM 0
Share

That should not be the case, the down value is only true for the frame when the button went down. There are three alternatives Get$$anonymous$$eyDown (gone down in current frame) Get$$anonymous$$ey (whether its just gone down or not) and Get$$anonymous$$eyUp (gone up in current frame) http://unity3d.com/support/documentation/ScriptReference/Input.Get$$anonymous$$eyDown.html

Show more comments
avatar image
0

Answer by DaveA · Mar 27, 2012 at 06:49 PM

http://answers.unity3d.com/search.html?redirect=search%2Fsearch&q=double+tap+key

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 Huntzie · Sep 14, 2015 at 04:40 PM 2
Share

Very unhelpful, thanks Dave!

avatar image
0

Answer by by0log1c · Mar 27, 2012 at 07:03 PM

It might not be the best implementation but this is pretty much the way I handle similar cases. I've wrote this for a dash and while its a little longer than I'd have expected, it works fairly well.

 float maxDelay = 0.5f;
 bool dashReady = false;
 
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.Space))
     {
         if(dashReady)
             Dash();
         else
             PrepareDash(true);
     }
 }

 void Dash()
 {
     dashReady = false;
     //dash action goes here
 }
 
 void PrepareDash(bool makeReady)
 {
     //this is where the handling happens
     CancelInvoke("CancelDash");
     Invoke("CancelDash",maxDelay);
     dashReady = true;
 }
 
 void CancelDash()
 {
     dashReady = false;
 }
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

13 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

Related Questions

Walking and Running Script not working. 1 Answer

Input.GetKey question 1 Answer

Update GetKey If Statements both playing instantly 1 Answer

Can/how do I set up a boolean to continue an animation after button press 1 Answer

OnGUI: += and -= operations problem 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