Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Strange issue with getting the y coordinates on input.mouseposition with touch

Discussion in '2D' started by kidgold, Jul 3, 2015.

  1. kidgold

    kidgold

    Joined:
    May 27, 2015
    Posts:
    1
    This is my first post here so I apologize for any poor formatting.

    I've been trying to get touch and drag controls working in my 2D game but have run into a strange issue. I never had this problem with 3D touch controls, so I'm a little lost.

    The issue is that when I use a device (android or iphone) to get input.mouseposition (or if I use input.GetTouch(0).position), the y coordinates are incorrect and different from if I use an actual mouse in the unity game preview. The controls are working perfectly in the unity preview, I can touch and drag objects exactly as intended. But on the device my touches are registering higher than when I am actually touching. In unity when using a mouse the 0,0 point is near the middle of the screen (lowered slightly), and the y coordinates range from -12 to 25. On my device there is no 0 point for the y axis as the y coordinates range from 25 to 65. The x coordinates are the same for both. The camera is orthographic.


    pos = Input.mousePosition;

    mouseWorldPos2D = Camera.main.ScreenToWorldPoint(pos);

    RaycastHit2Dhit = Physics2D.Raycast(mouseWorldPos2D, Vector2.zero);

    if (hit != null && hit.collider!=null) {
    do stuff
    }

    Thanks for any help! I'm a bit desperate.