- Home /
Question by
Copywright · Jan 31, 2013 at 04:07 AM ·
inputinputmanagerdouble-tap
Help with checking for a key tapped multiple times
Hey,
I'm trying to write a method that detects when a key is tapped twice within a certain range of time. Here's the non-functional code I have:
public bool isLookingRight;
public bool isLookingLeft;
public bool hasDoubleTapped;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public bool MultiTapKey (string keyName, int numberOfTaps, float rangeOfSeconds) {
if (cInput.GetKeyDown(keyName))
{
if ( rangeOfSeconds > 0 && numberOfTaps == 1)
{
hasDoubleTapped = true;
}
else
{
numberOfTaps += 1 ;
}
}
if ( rangeOfSeconds > 0 )
{
rangeOfSeconds -= 1 * Time.deltaTime ;
}
else
{
numberOfTaps = 0 ;
}
if (cInput.GetKeyUp(keyName))
{
hasDoubleTapped = false;
}
return hasDoubleTapped;
}
}
Can anyone help me out with a way to do this?
Comment
Answer by iwaldrop · Jan 31, 2013 at 04:23 AM
You should just check what time you last hit it.
public float interval;
private float lastTapTime;
private bool isDoubleTapped;
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
if (Time.time - lastTapTime < interval)
isDoubleTapped = true;
else
lastTapTime = Time.time;
}
if(isDoubleTapped)
{
print ("success");
isDoubleTapped = false;
}
}
}
Also, it's advisable that you call a method instead of setting and unsetting a flag; this is just an example.