Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Touch phase returning incorrect results

In Unity, 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.figerId);
            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;
        }
    }
}
like image 593
prolink007 Avatar asked Jun 13 '26 22:06

prolink007


1 Answers

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.

like image 65
Stefan Hoffmann Avatar answered Jun 17 '26 22:06

Stefan Hoffmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!