If i want a given function to start only when two different key inputs are clicked within a given timeframe, how would it be done?
As in you need to click “h” and “w” within a span of 5sec or else the function doesn’t start.
I thought of doing it with if statements and a delay/timer thing – but then it doesn’t take into account that you can go h->w and w->h, so I don’t like it.
Any ideas?
You had the right idea, but format it exactly how you want.
//UNTESTED.
JAVASCRIPT:
var timeframe : int = 0;
if(Input.GetKeyDown(KeyCode.H)){
timeframe += 1 * Time.DeltaTime; //add 1 per second
if(Input.GetKeyDown(KeyCode.W) && timeframe < 6){
//They managed to press both keys within 5 seconds
}
}
C#:
public int timeframe = 0;
if(Input.GetKeyDown(KeyCode.H)){
timeframe += 1 * Time.DeltaTime; //add 1 per second
if(Input.GetKeyDown(KeyCode.W) && timeframe < 6){
//They managed to press both keys within 5 seconds
}
}
And this would go in your UPDATE function, in either language, then really all youd need (which I am only just realizing I missed) is a way to reset the counter again, so you could just do a GetKeyUp function.
float timeframe = 0;
bool chkinput=false;
void Update ()
{
if(Input.GetKeyDown(KeyCode.H))
{
chkinput=true;
}
if(chkinput)
{
timeframe += 1 * Time.deltaTime;
if( Input.GetKeyDown(KeyCode.W))
{
print ("Input Sucess");
timeframe = 0;
chkinput=false;
}
if(timeframe > 10)
{
print ("Time Out");
timeframe = 0;
chkinput=false;
}
}
}