Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d How to detect taps on android?

Tags:

c#

unity3d

touch

Currently I am struggling to work out how to create a simple bit of code that will tell me if a tap has happened, allowing me to get the position of taps. Having looked through many different articles I am rather stumped as Touch.tapCount that is often suggested only works for ios. Also, i have tried to use Touch.deltaTime and/or Touch.deltaPosition to detect a tap but have failed.

I define a tap as having

  • A very short period between the finger initially touching and finally exiting the phone

  • Little or no movement

Thanks for reading this, I hope its clear and precise and if any detail or assistance is required on my behalf in order for your to answer feel free to ask. Any assistance is gratefully received. Thanks.

Note - I work in C#

like image 944
g_l Avatar asked Aug 02 '16 19:08

g_l


People also ask

How to detect tap on screen Unity?

To detect a touch in Unity it's quite simple we just have to use Input. GetTouch() and pass it an index.

What is input touchCount?

Input. touchCount provides the current number of screen touches. If Input. touchCount is greater than zero, the GetTouch index sets which screen touch to check.

What is touch phase unity?

TouchPhase is an enum type that contains the states of possible finger touches. The states represent each action the finger can take on the most recent frame update.


1 Answers

First you have to find out if your Android device indeed can register several touches. If you have a newer device, this shouldn't be a problem. I'm going to assume that your device can, and if it can not - you'll soon find out soon enough.

let's start with the update method.

void Update() {
// Nothing at the moment
}

What we first want to do is register touches. We can do this by putting a foreach inside, checking for touches in Input.touches. Like this:

    void Update() {
    foreach (Touch touch in Input.touches) {

    }
}

By doing this, we are always checking how many touches there are currently on the screen. What we can now do is check by fingerId, and if fingerId == 0, 1, 2... run some code. Here's what we got now:

    void Update() {
        foreach (Touch touch in Input.touches) {

        if (touch.fingerId == 0) {
            // Finger 1 is touching! (remember, we count from 0)
        }

        if (touch.fingerId == 1) {
            // finger 2 is touching! Huzzah!
        }
    }
}

We're great so far! What we now want to do is detect the motion we want. In our case, we wanted taps, right? That should work perfectly with TouchPhase Began, and Ended. There's also TouchPhase.Moved, but we don't need that now.

if (touch.fingerId == 0) {

    if (Input.GetTouch(0).phase == TouchPhase.Began) {
        Debug.Log("First finger entered!");
    }

    if (Input.GetTouch(0).phase == TouchPhase.Ended) {
        Debug.Log("First finger left.");
    }
}

Here we are checking the phase of the corresponding finger. If you run that now, you should be able to see the messages in the console whenever your first touch enters, as well as leaves the screen. This can be done with several touches, so here's the 'whole' script:

    void Update() {
    foreach (Touch touch in Input.touches) {

        if (touch.fingerId == 0) {
            if (Input.GetTouch(0).phase == TouchPhase.Began) {
                Debug.Log("First finger entered!");
            }
            if (Input.GetTouch(0).phase == TouchPhase.Ended) {
                Debug.Log("First finger left.");
            }
        }

        if (touch.fingerId == 1) {
            if (Input.GetTouch(1).phase == TouchPhase.Began) {
                Debug.Log("Second finger entered!");
            }
            if (Input.GetTouch(1).phase == TouchPhase.Ended) {
                Debug.Log("Second finger left.");
            }
        }
    }
}

I'm hoping this will help you. I'm fairly new at this myself, so if we're lucky - someone with more experience can come and help. I'm confident that this could be written a lot cleaner. Just remember that if you build it, you can't see the console messages. Check out Unity Remote if you haven't already. Good luck! :)

like image 123
Mothil Avatar answered Oct 15 '22 16:10

Mothil