1

On a 2D layout, having a fixed in place button-like GameObject that has a script with the OnMouseDown and OnMouseUp function: touching that and moving the finger away and releasing, can that OnMouseUp get that corresponding finger release position on screen?

The catch is: its a multiplayer, many other fingers on screen!

Programmer
  • 121,791
  • 22
  • 236
  • 328
Marco Antonio
  • 329
  • 3
  • 12

2 Answers2

1

You shouldn't be using the OnMouseDown and OnMouseUp callback functions for this because that would require that you get the touch position with Input.mousePosition on the desktops and Input.touches on mobile devices.

This should be done with the OnPointerDown and OnPointerUp functions which gives you PointerEventData as parameter. You can access the pointer position there with PointerEventData.position. It will work on both mobile and desktop devices without having to write different code for each platform. Note that you must implement the IPointerDownHandler and IPointerUpHandler interfaces in order for these functions to be called.

public void OnPointerDown(PointerEventData eventData)
{
    Debug.Log("Mouse Down:  " + eventData.position);
}

public void OnPointerUp(PointerEventData eventData)
{
    Debug.Log("Mouse Up: " + eventData.position);
}

If you run into issues with it, you can always visit the troubleshooting section on this post to see why.

Programmer
  • 121,791
  • 22
  • 236
  • 328
0

short answer yes. however this becomes a little muddled with multi-touch. basically you can get the mouse or finger position at any time. you can track it in real time if you want and display the position of the mouse at all times pretty easily. the Input.MousePosition variable is easy to grab. the problem comes when you have more than one mouse or finger in that you need to track which finger is up, and which ones are still down. still, if you set it up right it should work, it will just take more effort. my advice though,if your handling more than one touch, is to use the Standalone MultiTouch Input Module. its free in the asset store, and ive included a link for you. it's pretty straightforward.

Technivorous
  • 1,682
  • 2
  • 16
  • 22