- Home /
touch position to world position 2D
How can I transform the touch position (On phones) to world position. For instance if my phone's screen size is 1440 by 2560 and my touch position on the phone is X 600 Y 700. How can I transform that position to world position on unity?
I need that so I can know where the user put his finger.
Answer by robertbu · Oct 14, 2014 at 08:14 PM
There is no true 2D position. Even when using 2D features, positions are still 3D. For 2D, Orthographic camera, with rotation (0,0,0) you can do:
var pos = Camera.main.ScreenToWorldPoint(touchposition);
pos.z = transform.position.z;
This will be the finger position at the 'z' position of the object with the script.
You did not get me. I am using android, not pc. the x and y are different. I can not just put it in this code.
So you pass the position of the touch. You don't provide your touch code, so I cannot provide the exact code, but the concept is the same. You get a screen position and call ScreenToWorldPoint() to get the world position.
Answer by Digger8048 · Oct 14, 2014 at 10:23 PM
Vector3 screenPos = new Vector3(Input.mousePosition, Input.mousePosition, 10); Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
worldpos will be a world point where you touched, 10 meters from the camera.