Mouse up vs. touch up in Unity

10,447

Solution 1

I figured it out.

I was missing this call: Input.simulateMouseWithTouches = false;

Seems to work if you set this on a Start() event only (I could be wrong, it may be you need to set it before the first Input accessor on update, test it out I guess). Otherwise, Unity simulates mouse events from touch events. I can only speculate they do this in order to make it easier for game developers to write once and play cross-platform.

Note: Once its set, its set globally, so you’ll need to unset it even in a different scene you load later on.

Solution 2

For desktops:

  • Mouse Down - Input.GetMouseButtonDown
  • Mouse Up - Input.GetMouseButtonUp

Example:

if (Input.GetMouseButtonDown(0))
{
    Debug.Log("Mouse Pressed");
}

if (Input.GetMouseButtonUp(0))
{
    Debug.Log("Mouse Lifted/Released");
}

For Mobile devices:

  • Touch Down - TouchPhase.Began
  • Touch Up - TouchPhase.Ended

Example:

if (Input.touchCount >= 1)
{
    if (Input.touches[0].phase == TouchPhase.Began)
    {
        Debug.Log("Touch Pressed");
    }

    if (Input.touches[0].phase == TouchPhase.Ended)
    {
        Debug.Log("Touch Lifted/Released");
    }
}

Now, if you are having issues of clicks going through UI Objects to hit other Objects then see this post for how to properly detect clicks on any type of GameObject.

Share:
10,447
Alexandru
Author by

Alexandru

"To avoid criticism, say nothing, do nothing, be nothing." - Aristotle "It is wise to direct your anger towards problems - not people; to focus your energies on answers - not excuses." - William Arthur Ward "Science does not know its debt to imagination." - Ralph Waldo Emerson "Money was never a big motivation for me, except as a way to keep score. The real excitement is playing the game." - Donald Trump "All our dreams can come true, if we have the courage to pursue them." - Walt Disney "Mitch flashes back to a basketball game held in the Brandeis University gymnasium in 1979. The team is doing well and chants, 'We're number one!' Morrie stands and shouts, 'What's wrong with being number two?' The students fall silent." - Tuesdays with Morrie

Updated on June 05, 2022

Comments

  • Alexandru
    Alexandru almost 2 years

    I have a bit of an issue. I can't seem to find a good way to distinguish whether Unity is firing a mouse up vs. a touch up event. By touch up I mean when the user releases their finger from the screen after touching something on a touch display.

    The problem is this: I have an issue with game logic where the user both lets go of something they've selected with their mouse and SIMULTANEOUSLY touches / releases their touch to click a UI button (in other words, I'm trying to detect a mouse release only, or, a touch release only - how can I do that?). I've tried using both Input.GetMouseButtonUp(0) as well as Input.GetMouseButton(0), but both change their state if its a touch up or a mouse button up event.

    So how do you properly distinguish between the two? I even tried using the Touch.fingerId property which works well to track / distinguish ONLY touch events, but, the problem is then that I still cannot distinguish ONLY mouse up with Input.GetMouseButton(0) since it fires even in the event of a touch up.

    Does anyone out there happen to know what the proper way would be to detect just a mouse release, or just a touch release, separately, in Unity?

    Edit:

    Someone didn't understand the problem at hand, so let's assume for a second you have a desktop device with touch support. Add this script:

    void Update () {
        if (Input.GetMouseButtonDown(0))
            Debug.Log("Mouse button down.");
        if (Input.GetMouseButtonUp(0))
            Debug.Log("Mouse button up.");
    }
    

    If you click and HOLD with your mouse, while holding, you can touch and release with your finger, which will log Mouse button up., despite the fact that you did not release the mouse button. How can I distinguish between a mouse up vs. a touch release?

  • Alexandru
    Alexandru over 6 years
    You didn't understand the problem, so I will edit my question.
  • Alexandru
    Alexandru over 6 years
    The problem is desktop devices can support touch screens, so Input.GetMouseButtonUp(0) will actually evaluate as true if you release a touch while you hold the left mouse button down, but I need a way to determine if only the left mouse button was released exclusively.
  • Programmer
    Programmer over 6 years
    I've seen people using Input.GetMouseButton on both mobile and desktop. It may work but that is wrong. Notice how both examples are separated in my answer. I expect you to use Application.isMobilePlatform to determine which one to use during run-rime or use Unity's Directives to determine that during compile time.
  • Alexandru
    Alexandru over 6 years
    The only thing truly wrong here is that Unity by default makes touch events fire mouse functions in order to mimick mouse functions with very little documentation telling you that you need to disable this behaviour. This can create some nasty bugs on desktop devices with both a mouse and a touch display.
  • Programmer
    Programmer over 6 years
    It's actually a quite good feature. The idea of Unity is to make everything easy and it's working for them. This feature is good for new Unity users and also when making a prototype so that your simple code will work on mobile devices for testing purposes but once you get into handling multi-touches, you have to use the Touch class and separate the Input otherwise, you will run into issues.
  • Alexandru
    Alexandru over 6 years
    Its not a bad feature but I wish it would document this here (like a remark to explain what happens with touches and how to disable them): docs.unity3d.com/ScriptReference/Input.GetMouseButtonUp.html