Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

understanding the coordinate system of Input.GetTouch

Discussion in 'Scripting' started by protoben, Aug 17, 2015.

  1. protoben

    protoben

    Joined:
    Nov 11, 2013
    Posts:
    39
    Im getting strange results from a simple script. Im trying print to the console the coordinates where the touch event happens.

    While I am successfully printing the coordinates, I dont understand the coordinate system unity must be using to create them. For example, my top left click is around (-507.2, 452.6, 0.0), top right is (389.2, 487.7, 0.0), bottom right is (373.2, 62.2, 0.0) and bottom left is (-478.5, 35.1, 0.0)

    It seems like seems like the coordinate system is somewhere ~900 wide by ~420 tall. It also seems like the coordinate system is not centered as if I click on around the center of the screen I get coordinates like (-22.3, 291.7, 0.0)

    Other info about this project includes:
    • camera set to 0,0,-1000
    • there is an object set to 0,0,0 which looks like its in the center of my screen


    So the questions I have are:
    • what is the total size of the coordinate system being used?
    • Where is the zero zero point of the coordinate system?
    • Does the coordinate system change if the device changes?


    here is the script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class getTouchPosition : MonoBehaviour {
    5.  
    6.  
    7.     public Vector3 fingerPos;
    8.     public Vector3 worldPosition;
    9.     public GameObject[] particleList;
    10.    
    11.  
    12.     // Update is called once per frame
    13.     void Update ()
    14.         {
    15.         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
    16.             {
    17.                 fingerPos =  Input.GetTouch(0).position;
    18.                 print (fingerPos);
    19.                 actionFromTouch();
    20.         }
    21.  
    22.     }
    23.  
    24.  
    25.     void actionFromTouch(){
    26.         GameObject thing = Instantiate (particleList [0], fingerPos, Quaternion.identity) as GameObject;
    27.        
    28.     }
    29. }
    30.  
    thanks
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    This is definitely not normal behaviour- GetTouch returns screen-space coordinates, which should be 0,0 at the bottom left and Screen.width, Screen.height at the top right.

    Are you using Unity 4.5 or earlier? Which platform? Is this is a build or using Unity Remote?

    First blush I'd say upgrade Unity. There was a problem like this back in pre-4.6p1 releases that involve the orientation of the device being changed and the coordinate system turning wonky as a result.
     
    Last edited: Aug 17, 2015
  3. protoben

    protoben

    Joined:
    Nov 11, 2013
    Posts:
    39
    Thanks for answering Lysander,

    this is Unity 5.1.1
    unity remote 4
    iphone 6 with iOS 8.4
    and its on a mac running Yosemite.

    found demo project online where someone was trying to do something similar (spawn particle system at touch locations) and I get a perhaps similar/related bug where the visual orientation and the touch event orientation seem to get out of wack. So it will visually look landscape but the click events seem as if its squishing the touch coordinate system like perhaps the touch coordinate system is set to vertical or something.

    Here is the link to the project info and you can grab the project on github:
    http://pixelnest.io/tutorials/unity-touch-controls/

    thanks
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I don't have a device with iOS, only Android, so even if I could download and get it working properly, that may not help you. That said, I'm a bit curious, so I'll give it a shot and see if maybe it's a programming problem.

    The code that you posted isn't translating the screen space coordinates into world coordinates using Camera.main.ScreenToWorldPoint() before instantiating the effect, but you have a "worldPosition" vector, so I'm assuming you maybe oversimplified it for posting on the forum? That would not explain the print method getting negative numbers from the screen space position, so I didn't mention it.