public var keyTimer : float = 0; //Our timer
public var keyLength : float = 2; //The duration needed to trigger the function
public var isKeyActive : boolean = false;
function Update() {
//Initial key press
if (Input.GetKeyDown(KeyCode.Space) && !isKeyActive) {
//Get the timestamp
keyTimer = Time.time;
isKeyActive = true;
}
//Key currently being held down
if (Input.GetKey(KeyCode.Space) && isKeyActive) {
//Check is time elapsed is greater than keyLength
if (Time.time - keyTimer > keyLength) {
PlaySound();
isKeyActive = false;
}
}
//Key released
//This will not execute if the button is held (isKeyActive is false)
if (Input.GetKeyUp(KeyCode.Space) && isKeyActive) {
//Do something else
isKeyActive = false;
}
}