Question by
twonkkkkkkkkkk · Apr 02, 2020 at 06:23 PM ·
c#input
Telling the difference between a key press, double press and hold.
I've managed to get the code to where it does register a difference between a key press, hold and double press but whenever I double press it seems to register it twice. Would anyone be able to help? Thanks.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
float TimeKeep;//Time Since the button was pressed
float DPSense=0.015f;//how long it keeps checking for another press
float HoldSense=0.5f;//how long it keeps checking for another press
float PreviousPress = 0;//Keeps Track of the amount of resses
int Press = 0;//Keeps Track of the amount of presses
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.R))//While its held down it adds one to the variable
{
TimeKeep += Time.deltaTime;
}
if (Input.GetKeyUp(KeyCode.R))
{
if (TimeKeep < HoldSense)
{
if(TimeKeep - PreviousPress <= DPSense)
{
Debug.Log("Double Press");
Press = 0;
}
else
{
Debug.Log("Press");
Press += 1;
PreviousPress = TimeKeep;
}
}
else
{
Debug.Log("Held");
Press += 1;
PreviousPress = TimeKeep;
}
TimeKeep = 0;
}
}
}
problem.png
(8.5 kB)
Comment
Your answer
Follow this Question
Related Questions
Making an object move non stop after button input? 1 Answer
Help with improving, dynamisation of Input code 1 Answer
Unable to assign devices to PlayerInput whit Instantiate() 1 Answer
Unity Input.GetMouseButtonDown(0) not working 0 Answers
What can I do to get Unity to understand the Debug.Drawline command? 1 Answer