Unity Android C# Click Tracking

Question:

It is necessary that when you click on the screen, a function is executed, and only once, until you click again (as in FlappyBird). At me, when you touch the screen, it runs until you release your finger. Used Input.touchCount>0

Answer:

It is obvious that it is not enough to track only that there is a click. We still need to track the phase.

TouchPhase – list of possible phases

Began   - Палец коснулся экрана.
Moved   - Палец передвинулся по экрану.
Stationary  - Палец коснулся экрана, но не сдвинулся.
Ended   - Палец только что оторван от экрана. Это последняя фаза нажатий.
Canceled    - Система отменила отслеживание касаний.

An example can be seen there: https://docs.unity3d.com/ru/current/ScriptReference/Touch-phase.html

And finally write something like:

void Update() {        
    if (Input.touchCount > 0) {
        Touch touch = Input.GetTouch(0);
        if (touch.phase == TouchPhase.Began) {
            // что-то делать
        }
    }        
}
Scroll to Top