Question by
Humanhoney · Mar 18, 2016 at 03:35 PM ·
timeinput.getkey
few buttondown in fixed time
Hi. Can anybody help with the next problem. Need method whick returns true if some button was pressed in needed time. Example what i mean:
private bool PressedTwice(KeyCode button, float DelayBetweenButtonDowns) { if(///Button was clicked twice in this time period///) {return true;} else{return false;}
}
Also how can i get if button was clicked in last 1sec for example?
Comment
Best Answer
Answer by Jessespike · Mar 18, 2016 at 05:54 PM
using UnityEngine;
using System.Collections.Generic;
public class DoubleKeyPressTest : MonoBehaviour {
Dictionary<KeyCode, float> KeyPressTimes = new Dictionary<KeyCode, float>();
// Update is called once per frame
void Update ()
{
KeyCode testKey = KeyCode.A;
float testTime = 1f;
if (PressedTwice (testKey, testTime))
{
Debug.Log("pressed twice returned true");
}
}
private bool PressedTwice(KeyCode key, float timeRangeForDoublePress)
{
bool result = false;
if (Input.GetKeyDown(key))
{
if (KeyPressTimes.ContainsKey(key))
{
if (Time.time - KeyPressTimes[key] < timeRangeForDoublePress)
{
result = true;
}
Debug.Log("Time between key presses " + key + " is " + (Time.time - KeyPressTimes[key]) + " seconds");
KeyPressTimes[key] = Time.time;
}
else
{
KeyPressTimes.Add(key, Time.time);
}
}
return result;
}
}
Your answer
Follow this Question
Related Questions
Destroying bullets after a certain amount of time 1 Answer
make enemy cars spawn faster over time! 2 Answers
Problem with a stopwatch 0 Answers
Instantiate objects in for loop 0 Answers