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?

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.

// 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;
		}
	}
}

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

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;
    }
    
}

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;
        }
    }

This is the code I use to teleport the player’s character when the “W” key is double-tapped:

public void checkDoubleTap () {
if (Input.GetKeyUp (KeyCode.W) && previousKey == “W” && (Time.time - firstTapTime) <= 0.2f) {
playerTeleport ();
return;
}
if (Input.GetKeyUp (KeyCode.W)) {
previousKey = “W”;
firstTapTime = Time.time;
return;
}
}

I don’t like how this code seems too cluttered to me, but for people who need it, this lets you check if a button was pressed once, twice or if it is being held and when it’s released after holding the button.

    int ButtonCount = 0;
    float t;
    float holdThres = .8f; // threshold for checking if button is being held
    float doubleTrhes .5f; //threshold for checking if button is being pressed twice
    void Update () {

        string button = "Fire 1";

        if (Input.GetButtonDown (button)) {
            if (ButtonCount == 0) {
                t = Time.time;
            }
            ButtonCount += 1;
        }

        if ((Time.time - t) > holdThres  && Input.GetButton (button) && ButtonCount == 1) {
            Debug.Log ("Is currently holding [" + button + "]");
        } else if ((Time.time - t) > holdThres  && Input.GetButtonUp (button) && ButtonCount == 1) {
            Debug.Log ("Stopped holding [" + button + "]");
            ButtonCount = 0;
        } else if ((Time.time - t) < doubleTrhes  && ButtonCount == 2) {
            Debug.Log ("Double tapped [" + button + "]");
            ButtonCount = 0;
        } else if ((Time.time - t) > doubleTrhes  && ButtonCount == 1 && !Input.GetButton (button)) {
            Debug.Log ("Tapped [" + button + "] once");
            ButtonCount = 0;
        }
    }