# Touch Screen pos to world pos.

I searched several codes and tried them but something weird.

Here is my code.

``````// Update is called once per frame
void Update ()
{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
print ("Touch Screen : " + Input.GetTouch(0).position);
Vector3 Pos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
print("Pos " + Pos);
}
}
``````

The weird thing is that ‘Input.GetTouch(0).position’ is change whenever I touch different place but ‘pos’ is always point same position.

I debugged using unity remote in android.

I want to know why this happen.

A point in the 2D screen corresponds to an infinite line in the 3D world - you must specify the distance from the camera in the Z coordinate passed to ScreenToWorldPoint. Since you’re passing GetTouch(0).position, Z is zero and the point returned is the position of the camera. You could pass a constant distance like Z = 10, for instance, but this would always return points at 10 units from the camera. A more common solution is to define a logical plane and do a Plane.Raycast - if you want to select a point on the ground, for instance, create an horizontal plane, like this:

``````void Update ()
{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
// create ray from the camera and passing through the touch position:
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
// create a logical plane at this object's position
// and perpendicular to world Y:
Plane plane = new Plane(Vector3.up, transform.position);
float distance = 0; // this will return the distance from the camera
if (plane.Raycast(ray, out distance)){ // if plane hit...
Vector3 pos = ray.GetPoint(distance); // get the point
// pos has the position in the plane you've touched
}
}
}
``````