-1
\$\begingroup\$

I am encountering an extremely absurd phenomenon. When I run this code on my smartphone via Unity Remote or even on the computer, it works as expected. But when I install the APK on the device and run it, the touch distance changes! Does this have something to do with resolution and Vector3.Distance?

 distance = Vector3.Distance (Input.mousePosition, button.position); Debug.Log (distance); if (Input.GetMouseButton (0) && distance > 55) { // do something } 
\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

Actually,

Input.GetMouseButtonDown returns true during the frame the user pressed the given mouse button.

If you want to get mouse position on click then you can use Input.mousePosition in Input.GetMouseButtonDown is true, it will return mouse position in pixel coordinates.

Update() { if (Input.GetMouseButtonDown(0)) Debug.Log("Mouse position: " + Input.mousePosition); } 

Edit: It also works like a charm on touch devices. For single touch on touch devices you can use it without any doubt.

Edit2: As I mentioned that Input.mousePosition will give you pixel coordinates not world coordinates. Basically you are comparing Pixel point and World point, that'd obviously not gonna do what you want. But you can simply convert screen point to world point through ScreenToWorldPoint. Like,

Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); distance = Vector3.Distance (mouseWorldPosition , button.position); ... 
\$\endgroup\$
6
  • \$\begingroup\$ As I said, "I want to publish for smartphones, touch devices so I can't use Input.mousePosition" I already tested it and it doesn't work. \$\endgroup\$ Commented Mar 12, 2016 at 20:24
  • \$\begingroup\$ FYI, it is perfect for even touch devices. For single touch, it is perfect. You may have a look at this game. I did use Input.GetMouseButton and Input.mousePosition only and you can see, everything is working fine. \$\endgroup\$ Commented Mar 12, 2016 at 21:12
  • \$\begingroup\$ I substantially edited the question. There's something wrong with the way Vector3.Distance measures distance. \$\endgroup\$ Commented Mar 12, 2016 at 21:33
  • \$\begingroup\$ @Dct4 This is totally a different question bro, well, check the edits \$\endgroup\$ Commented Mar 12, 2016 at 21:54
  • \$\begingroup\$ Your new edit still gets me the same problem. Review it, please :) \$\endgroup\$ Commented Mar 12, 2016 at 22:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.