- Home /
Question by
lioneleonn · Mar 18, 2020 at 11:42 PM ·
inputcontrolskeyboard inputfightingfightingame
Single tap / Double tap / Hold button
Hi. Im making a fighting game and Im having some difficulties with the inputs. Thats because I wanna put 3 kinds of actions for each button:
Single tap button Double tap button Hold button
I made some test code, however, I wanna to decrease the delay between the button input and the action, because it is a fighting game and I cannot have that amount of delay.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controls : MonoBehaviour
{
float tapDuration;
int tapCount;
void Update() {
if(Input.GetKeyDown(KeyCode.A)){ //if there is any touch
tapDuration += Time.deltaTime;
if (Time.deltaTime - tapDuration < 0.2f) {
tapCount++;
}
StartCoroutine(singleOrDoubleOrHold(KeyCode.A));
}
}
IEnumerator singleOrDoubleOrHold(KeyCode pressedKey){
yield return new WaitForSeconds(0.4f);
if((tapCount == 1 || tapCount > 2) && !Input.GetKey(pressedKey)) {
Debug.Log ("Single");
}
else if(tapCount == 2){
//this coroutine has been called twice. We should stop the next one here otherwise we get two double tap
StopCoroutine("singleOrDoubleOrHold");
if (!Input.GetKey(pressedKey)) {
Debug.Log ("Double");
} else {
Debug.Log ("Double Hold");
}
} else if ((tapCount == 1 || tapCount > 2) && Input.GetKey(pressedKey)) {
Debug.Log ("Hold");
}
tapCount = 0;
tapDuration = 0f;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612225145im_/https://answers.unity.com/themes/thub/images/avi.jpg)