- Home /
Touch phase returning incorrect results
I am attempting to use Touch
.
I am having non-stop issues with it and have decided to just try and get something simple working first.
Whenever i press the screen, i am expecting Touch.phase
to return TouchPhase.Began
every time. However, it will sometimes return TouchPhase.Stationary
when touching the screen for the first time. I have also noticed that it will sometimes not call TouchPhase.Ended
or TouchPhase.Canceled
when the touch has left the screen. What am i doing wrong?
Here is my very simple code:
void FixedUpdate() {
if(Input.touchCount == 1) {
Touch touch = Input.GetTouch(0);
switch(touch.phase) {
case TouchPhase.Began:
Debug.Log("Began: " + touch.fingerId);
break;
case TouchPhase.Stationary:
Debug.Log("Stationary");
break;
case TouchPhase.Moved:
Debug.Log("Moved");
break;
case TouchPhase.Canceled:
Debug.Log("Canceled");
break;
case TouchPhase.Ended:
Debug.Log("Ended");
break;
default:
Debug.Log("Default");
break;
}
}
}
Touches should be done in Update, not fixed update. Also, are you doing anything with more than one finger at a time? You're checking if touch count is 1, so if you put one finger down and it's reading correctly, then you stick a second finger down, it will stop checking the touches because there's two. Just my observations but I don't really know how you're testing this
Just one touch. I am testing it by pressing the screen and watching the output. I was going through one of the tutorials that talk about having keyboard input detection within fixedUpdate. I was looking at the space shooter and the PlayerController. So, i just tried using it with touches on my android device.
Putting it into update()
did the trick. Thanks for the advice. If you create an answer with that response, i will accept it as the answer. If you could also include some information regarding why touch should not be in fixedUpdate()
, that would be awesome. Thanks
Answer by prolink007 · Apr 10, 2014 at 01:09 PM
Credit: Slobdell and Leosori (stackoverflow) for the answer and explanation.
Touch.phase
is updated every frame, so you have to check it within the Update()
method.
FixedUpdate()
is only called every x frames (fixed framerate), that's the reason you are missing some of the touch phases.