Simplifying double tap script

I have implemented a double tap run script in my game and it works but I need to simplify it either creating 4 functions for each direction or creating a single new function but still pass the same variables.

Current script.

 // dash settings
    double buttonrefreshd = 0.5;
    int buttontapd = 0;
    double buttonrefreshl = 0.5;
    int buttontapl = 0;
    double buttonrefreshr = 0.5;
    int buttontapr = 0;
    double buttonrefreshu = 0.5;
    int buttontapu = 0;
    float oldspeed;

void Start()
    {
        // get old movement speed
        mv_Model = GetComponent<CharacterMovementModel>();
        oldspeed = mv_Model.speed;
    }

    void Update()
    {
        // stop moving if opposite key is pressed
        if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.RightArrow))
        {
            can_move = false;
        }
        if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.UpArrow))
        {
            can_move = false;
        }
        if (Input.anyKey == false)
        {
            can_move = true;
        }
        // Resume moving if opposite key is released
        if (Input.GetKeyUp(KeyCode.DownArrow) || Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.UpArrow))
        {
            can_move = true;
        }
        // Allow movement if no key is pressed
        if (Input.anyKey == false)
        {
            can_move = true;
            isMoving = false;
        }

        UpdateDirection();
    }

void UpdateDirection()
    {
        Vector2 newDir = Vector2.zero;
        mv_Model = GetComponent<CharacterMovementModel>();

        // double tap dash
        // down
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            // double tap running
            if (buttonrefreshd >= 0 && buttontapd == 1)
            {
                mv_Model.speed = 9;
            }
            else
            {
                buttonrefreshd = 0.5;
                buttontapd += 1;
            }
        }
        else if (Input.anyKey == false)
        {
            // if a button hasn't been pressed reset the tap count
            if (buttonrefreshd > 0)
            {
                buttonrefreshd -= 1 * Time.deltaTime;
            }
            else
            {
                mv_Model.speed = oldspeed;
                buttontapd = 0;
            }
        }
        // left
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            // double tap running
            if (buttonrefreshl >= 0 && buttontapl == 1)
            {
                mv_Model.speed = 9;
            }
            else
            {
                buttonrefreshl = 0.5;
                buttontapl += 1;
            }
        }
        else if (Input.anyKey == false)
        {
            // if a button hasn't been pressed reset the tap count
            if (buttonrefreshl > 0)
            {
                buttonrefreshl -= 1 * Time.deltaTime;
            }
            else
            {
                mv_Model.speed = oldspeed;
                buttontapl = 0;
            }
        }
        // right
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            // double tap running
            if (buttonrefreshr >= 0 && buttontapr == 1)
            {
                mv_Model.speed = 9;
            }
            else
            {
                buttonrefreshr = 0.5;
                buttontapr += 1;
            }
        }
        else if (Input.anyKey == false)
        {
            // if a button hasn't been pressed reset the tap count
            if (buttonrefreshr > 0)
            {
                buttonrefreshr -= 1 * Time.deltaTime;
            }
            else
            {
                mv_Model.speed = oldspeed;
                buttontapr = 0;
            }
        }
        // up
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            // double tap running
            if (buttonrefreshu >= 0 && buttontapu == 1)
            {
                mv_Model.speed = 9;
            }
            else
            {
                buttonrefreshu = 0.5;
                buttontapu += 1;
            }
        }
        else if (Input.anyKey == false)
        {
            // if a button hasn't been pressed reset the tap count
            if (buttonrefreshu > 0)
            {
                buttonrefreshu -= 1 * Time.deltaTime;
            }
            else
            {
                mv_Model.speed = oldspeed;
                buttontapu = 0;
            }
        }
        // don't allow diagonal taps
        if (isMoving == true)
        {
            buttonrefreshd -= 1 * Time.deltaTime;
            buttonrefreshl -= 1 * Time.deltaTime;
            buttonrefreshr -= 1 * Time.deltaTime;
            buttonrefreshu -= 1 * Time.deltaTime;
        }
}

Any help is appreciated, just trying to reduce the code as much as possible.
The reason why I have 4 refresh and tap variables is to basically prevent running when I’m walking diagonally but can still run diagonally.

Hey there. Right away I see that your “can_move” is doing nothing (at least nothing noticeable that I can see here.
You needn’t multiply by ‘1’ with Time.deltaTime ever :slight_smile: You can, it will always be the same as without, though.
You needn’t check " Input.anykey " multiple times. It will always report the same value in each frame, so you can include everything you want in its one check.

// this might help
bool up, down, left, right;
if(Input.GetKeyDown(KeyCode.DownArrow)){
// your logic
down = true;
}
// etc..
// later on
if (!(up && down)) {
if(up) {
// check time is okay and execute
}
else if(down) // the same
}

My first thought would be to use a dictionary to store the “last time pressed” of all the buttons you’re checking for. This would allow you to merge your double-tap detection code into one “DidDoubleTap” function, and just call that for each Keycode you want. And by storing the last time it was pressed, rather than constantly mathing with Time.deltaTime, you can get a slightly more efficient and reliable script in the process. (e.g. imagine if this script got disabled for a time; if you used Time.deltaTime, then a tap just before and just after the disabling could be misinterpreted as a double-tap, no matter how long the script was disabled).

private Dictionary<KeyCode, float> timeLastTapped = new Dictionary<KeyCode, float>();
public float doubleTapInterval = 0.5f;

public bool DidDoubleTap(KeyCode k) {
if (!timeLastTapped.ContainsKey(k) ) timeLastTapped.Add(k, -9999f);
bool pressed = Input.GetKeyDown(k);
float previousTap = timeLastTapped[k];
if (pressed) timeLastTapped[k] = Time.time;
return (pressed && Time.time < previousTap + doubleTapInterval);
}

void Update() {
if (DidDoubleTap(KeyCode.DownArrow) ) {
//do double-tap things
}

Will check out when I get off work, checked out dictionary from my c# reference actually appears to be what I need, will find out in about…7hrs.