- Home /
 
Double Clicking Horizontal/Vertical Axis
Hey! I have been trying to make a dodgeing system using double taps on the arrow keys. I tried the script here: http://answers.unity3d.com/questions/340593/how-do-i-make-a-double-tap-system-for-dashing.html but for some reason the debug is also called by simply holding the buttons down. Anybody know what im doing wrong?
 void Update () 
     {
     if(Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Vertical") > 0 && IngameGUI.Instance.isInMenu == false)
         {
            if(ButtonCooler > 0 && ButtonCount == 1/*Number of Taps you want Minus One*/)
             {
             Debug.Log("Double Taped");
              //Has double tapped
               }
             else
             {
             ButtonCooler = 0.5f; 
             ButtonCount += 1 ;
              }
    }
    if(ButtonCooler > 0)
    {
  
       ButtonCooler -= 1 * Time.deltaTime ;
  
    }else
     {
       ButtonCount = 0 ;
       }
 }
 
              Answer by ICHeeryI · Jan 11, 2014 at 02:26 PM
Im not sure, but I think that you want something like this.. http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKey.html
     var keyA : int = 0;
     if(Input.GetKeyDown(Keycode.A)){ //We pressed key A
     if(keyA ==0)keyA = 1; //If keyA is 0 then set it to one
     else(keyA == 1)keyA =2; //If keyA is 1 then set it to two
     }
     else if(keyA==2){
     //Double tapped so we reset
     keyA =0;
     }
     else{
     //This mean there is only 1 key pressed or no key is pressed
     //You need to add time here, after 1 sec keyA will be again 0
     }
 
               Axis code(Double tap)(JS):
 var hzH : int = 0;//Axis
 var hzSwitch : boolean = false; //Switch
 var pressedTime : float; //Time
 
 function Update () 
     {
     //If pressed twice
     if(hzH==2){
     Debug.Log("HE PRESSED IT TWICE :O"); //Random text ;)
     pressedTime = 0; //Return time value
     hzH=0; //Return press value
     }
     if(hzH==1){ //If we pressed once count down seconds
         pressedTime += Time.deltaTime;
     }
     //We pressed anything?
     if(Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
        {       
            if(Input.GetAxis("Horizontal") >0 && hzH<2){
                    hzSwitch = true; //We preseed horinzontal
            }
           }
         else if (hzSwitch && pressedTime<=2){ //Time is not up? We must press again that axis to turn on hzSwitch
                if(hzH==0){hzH=1;hzSwitch = false;}
                else if(hzH==1){hzH=2;hzSwitch = false;}
       }
        if(hzH==1 && pressedTime>=2){ //We only pressed it once and time is up
         pressedTime=0;
         hzH=0;
       }
 }
 
              Thanks for your reply! So how would this work for the axis? Because as far as I know they are not a key (I might be wrong)
Best method is to use something that tell you what button is pressed, axis doesn't have that. But anyway I assume that you love using axis ;). I updated my answer with a new code. As you can see there is so much going on. That's why is better to use getkeydown. Anyway, GoodLuck!
Ahhh I already like you :D Okay I will check it out thank you very much! I'll leave a tick if it works
Okay so unfortunately, it turns out that the script does not work. I have added all kinds of debugs and it turns out that the line for when it is pressed twice does get called occasionally, but not due to a double tap (in fact, I don't even know how its called). Anyway, here is the code I have with debugs, maybe I did something wrong :P
 if(hzH == 2)
         {
         Debug.Log("HE PRESSED IT TWICE :O"); //Random text ;)
             pressedTime = 0; //Return time value
            hzH = 0; //Return press value
            }
     if(hzH == 1)
         { //If we pressed once count down seconds
         pressedTime += Time.deltaTime;    
         }
     //We pressed anything?
     if(Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Vertical") > 0)
        {       
            if(Input.GetAxis("Horizontal") > 0 && hzH < 2)
                 {
                 hzSwitch = true; //We preseed horinzontal
                    }
         else if (hzSwitch && pressedTime <= 2)
         { //Time is not up? We must press again that axis to turn on hzSwitch
             if(hzH == 0)
             {
                 hzH = 1;
                 hzSwitch = false;
             }
             else if(hzH == 1)
             {
                 hzH = 2;
                 hzSwitch = false;
             }
        }
        }
        if(hzH == 1 && pressedTime >= 2)
         { //We only pressed it once and time is up
        pressedTime=0;
        hzH=0;
        }
 
                 Check twice :P. Remove bracket at line 30, and add it after line 17.
Your answer