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
2
Question by MilkX5 · Oct 31, 2012 at 03:18 AM · animationruntapdashdouble

How do I make a double-tap system for dashing

I want to make a dash system like in kirby and smash bros (i'm making a fighter game combining both). I'm having difficulties going from a walking animation, to a running animation when a right or left key is tapped twice. Here is the code I have so far:

 var keyCounter : int = 0;
 var keyUp : boolean = false;
 var running : boolean = false;
 var walking : boolean = false;
 
 
 
 function Update () 
 {
     var aniPlay = GetComponent("aniSprite");
     
     keyCounter = 0;
     
 
     if (Input.GetKey("right") && !running ) //walk right
     {
         print ("keyCounter = " + keyCounter);
         aniPlay.aniSprite (24,26,0,1.75,12,15);
         keyCounter = 1;
         walking = true;
     }
     if (Input.GetKeyUp ("right") && keyCounter == 1)
         {
             if ( Time.time < 0.5 ) //tapped within half a second
             {
                 print ("keyCounter = " + keyCounter);
                 keyCounter = 2;
                 walking = false;
             }
             else  //cancel walk/run
             {                
                 print ("keyCounter = " + keyCounter);
                 walking = false;
                 keyCounter = 0;
             }
         }
     if (Input.GetKeyDown("right") && keyCounter == 2)
     {
             
         if (Input.GetKey ("right") && !walking) 
         {
             running = true;
             aniPlay.aniSprite (24,26,1,3.25,7,15);
             print ("i am running  " + keyCounter);
         }
         if (Input.GetKeyUp ("right") && keyCounter == 2) // run right
         {
             print ("keyCounter = " + keyCounter);
             running = false;
             keyCounter = 0;
         }
     }
 }

I've looked at other examples of a double tap but it still hasn't helped me. Any suggestions or tips on how to fix this?

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

7 Replies

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

Answer by Montraydavis · Oct 31, 2012 at 04:34 AM

 var ButtonCooler : float = 0.5 ; // Half a second before reset
 var ButtonCount : int = 0;
 function Update ( )
 {
    if ( Input.anyKeyDown ( ) ){
 
       if ( ButtonCooler > 0 && ButtonCount == 1/*Number of Taps you want Minus One*/){
          //Has double tapped
       }else{
         ButtonCooler = 0.5 ; 
         ButtonCount += 1 ;
       }
    }
 
    if ( ButtonCooler > 0 )
    {
 
       ButtonCooler -= 1 * Time.deltaTime ;
 
    }else{
       ButtonCount = 0 ;
    }
 }

That detects if you double tap any two keys within 0.5 seconds. Change it to fit your needs.

Comment
Add comment · Show 8 · 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 Montraydavis · Oct 31, 2012 at 06:09 AM 0
Share

If this has answered your issue, please hit the check mark to represent as answered. Thanks for supporting Unity3D ! :D

avatar image MilkX5 · Nov 01, 2012 at 11:56 PM 1
Share

@ $$anonymous$$ontraydavis Thank you so much! I'll give you credit for this code in my game :)

avatar image Montraydavis · Nov 02, 2012 at 12:02 AM 0
Share

Thank you. And you are very welcome. If you ever need help, feel free to ask at ANYTI$$anonymous$$$$anonymous$$ Let me know the progress on your game. I want to see how it looks once you get to a beta point, if you don't $$anonymous$$d. Best of luck to you, and thanks for using Unity3D.!

--PS: I didn't mention in the post above, but, you can change the number of taps you want at ( == 1 ) . It will always be one less than what you want. I just updated the code .

avatar image MilkX5 · Nov 02, 2012 at 12:12 AM 0
Share

You will definitely be first on my list for my beta game!

avatar image elopez7 · Mar 26, 2014 at 01:52 PM 1
Share

Dude, I hope you don't $$anonymous$$d, but I was having a similar problem creating something similar. I am working in a beat'em up kind of game and I needed to create something for combo chains. Your code gave me the ground I needed to work with, although I work in C#, so I did some changes to it and made into a triple click thing where each time you click something different is going to happen. I will post it, since I could not have it done it without yours and in case someone else needs help with something similar. Without further ado here we go.

//Originally written by $$anonymous$$ontraydavis //Code taken from the answers forums in the Unity website //Originally it was written in javaScript, but I changed into C# //It is also worth nothing that it was originally meant to detect double click actions //Therefore I changed it to be used to create a combo chain kind of thing //It will detect triple clicks //In other words, each time you click a different action will be performed

 private int clickCount = 0;
 private int mouseTimer = 0;    
 
 void attackTest()
     {
         if(Input.GetButtonDown("Fire1"))
         {
             if(clickCount == 0)//Number of tabs you want $$anonymous$$us one
             {
                 anim.SetTrigger("Attack");
             }
 
             if(mouseTimer > 0 && clickCount == 1)//Number of tabs you want $$anonymous$$us one
             {
                 anim.SetTrigger("Attack01");
             }
          
             if (mouseTimer > 0 && clickCount == 2)//Number of tabs you want $$anonymous$$us one
             {
                 anim.SetTrigger("Attack02");
             }
           
             else
             {
                 mouseTimer = 0.5f;
                 clickCount += 1;
             }
         }
 
         if(mouseTimer > 0)
         {
             mouseTimer -= 1 * Time.deltaTime;
         }
         else
         {
             clickCount = 0;
         }
     }
Show more comments
avatar image
3

Answer by syamilsynz · Feb 22, 2014 at 09:14 PM

 // double tap variable
 var time1: float;
 var time2: float;
 var isTap : boolean = false;
 
 function Update () 
 {
     if (Input.GetMouseButton(0)) // when mouse is clicked
     {
         if (isTap == true)
         {
             time1 = Time.time;
             isTap = false;
             
             if (time1 - time2 < 0.2f) // interval between two clicked
             {
                 // double tap occur here
             }
         }
     }
     else // first of all, enter here because the mouse is not clicked
     {
         if (isTap == false)
         {
             time2 = Time.time;
             isTap = true;
         }
     }
 }
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
1

Answer by Kerihobo · Feb 18, 2015 at 01:02 AM

http://aidtech-game.com/double-tap-button-unity3d/#.VOPjffmUd8E

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
1

Answer by ajnaduvil · Oct 13, 2015 at 03:40 PM

Here is my solution

using UnityEngine; using System.Collections;

 public class TapDetector : MonoBehaviour {
 
     public bool singleTap = false;
     public bool doubleTap = false;
 
     bool tapping = false;
     float tapTime = 0;
     float duration = .4f;
     
 
 
 
 
 void Start()
     {
         
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             if (tapping)
             {
                 doubleTap = true;
                 Debug.Log("DoubleTap");
                 tapping = false;
             }
             else
             {
                 tapping = true;
                 tapTime = duration;
                 
             }
         }
         if (tapping)
         {
             tapTime = tapTime - Time.deltaTime;
             if (tapTime <= 0)
             {
                 tapping = false;
                 singleTap = true;
                 Debug.Log("SingleTap");
             }
 
         }
 
     }
     void LateUpdate()
     {
         if (doubleTap) doubleTap = false;
         if (singleTap) singleTap = 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
avatar image
0

Answer by streeter12 · Nov 13, 2015 at 01:55 PM

You can use this class with any key and deltatime. You can increase perfomance using Time.timeSinceLevelLoad instead Time.deltaTime, but you will have float accuracy problem after 115 days of playing)

 private DoubleClicker tabDoubbleCatch = new DoubleClicker(KeyCode.Tab);
 
  private void Update()
         {
             if (tabDoubbleCatch.DoubleClickCheak())
             {
                //some code
             }
          }
 
  public class DoubleClicker
     {
         /// <summary>
         /// Construcor with keycode and deltaTime set
         /// </summary>
         public DoubleClicker(KeyCode key, float deltaTime)
         {
             //set key
             this._key = key;
 
             //set deltaTime
             this._deltaTime = deltaTime;
         }
 
         /// <summary>
         /// Construcor with defult deltatime 
         /// </summary>
         public DoubleClicker(KeyCode key)
         {
             //set key
             this._key = key;
         }
 
         private KeyCode _key;
         private float _deltaTime = defultDeltaTime;
 
         //defult deltaTime
         public const float defultDeltaTime = 0.3f;
 
         /// <summary>
         /// Current key property
         /// </summary>
         public KeyCode key
         {
             get { return _key; }
         }
 
         /// <summary>
         /// Current deltaTime property
         /// </summary>
         public float deltaTime
         {
             get { return _deltaTime; }
         }
 
         //time pass
         private float timePass = 0;

         /// <summary>
         /// Cheak for double press
         /// </summary>
         public bool DoubleClickCheak()
         {
             if (timePass > 0) { timePass -= Time.deltaTime; }
 
             if (Input.GetKeyDown(_key))
             {
                 if (timePass > 0) { timePass = 0; return true; }
 
                 timePass = _deltaTime;
             }
 
             return 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
  • 1
  • 2
  • ›

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

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

Related Questions

play animation when moving,and idle animation when stand still 1 Answer

Mid air "sprint" 2 Answers

Can the animation editor create local rotational data? 3 Answers

Enemy animtion and movement 0 Answers

Double tap to dash (With Multiple keypress) 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