How would i do a long press?
My algorithm i am trying to do goes like this (but doesnt seem to work)
if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
{touch0BeginTime = Time.time;}
if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary
&& (Time.time - touch0BeginTime))
{//long press code here}
Why doesn’t this work?
Try this:
private var longPressDetected: boolean = false;
private var newTouch: boolean = false;
private var touchTime: float;
function Start () {
}
function Update () {
if (Input.touchCount == 1){
var touch: Touch = Input.GetTouch(0);
if(touch.phase==TouchPhase.Began){
newTouch = true;
touchTime = Time.time;
Debug.Log('touchTime='+touchTime);
}else if (touch.phase == TouchPhase.Stationary){
if(newTouch==true && Time.time-touchTime>1){
Debug.Log('longpress detected');
newTouch = false;
longPressDetected = true;
}else if (newTouch == false && longPressDetected==false){ // began not detected
newTouch = true;
touchTime = Time.time;
Debug.Log('touchTime='+touchTime);
}
}else{
newTouch = false;
longPressDetected = false;
Debug.Log('setting newTouch false');
}
}
}
DaveA
2
(Time.time - touch0BeginTime) will be non-zero, hence ‘true’ in the next frame. You need to compare it to some real amount of time, like
var longPressTime = 2.0;
(Time.time - touch0BeginTime) > longPressTime