- Home /
Camera.main.ScreenToWorldPoint return camera position
Iam trying to get world co-ordinate from my screen touch position, all i have is a cube in my scene and i am attaching a script to it which will return world co-ordinate, here is the update function from script.
function Update () {
for (var touch:Touch in Input.touches)
{
Debug.Log(touch.position +" "+Camera.main.ScreenToWorldPoint(Vector3(touch.position.x,touch.position.y,0f)));
}
}
and here is the kind of output iam getting
(135.9, 235.5) (0.0, 18.0, 0.0)
UnityEngine.Debug:Log(Object)
TestIos:Update() (at Assets/Scripts/TestIos.js:26)
(154.1, 226.7) (0.0, 18.0, 0.0)
UnityEngine.Debug:Log(Object)
TestIos:Update() (at Assets/Scripts/TestIos.js:26)
(162.6, 223.1) (0.0, 18.0, 0.0)
UnityEngine.Debug:Log(Object)
TestIos:Update() (at Assets/Scripts/TestIos.js:26)
what is the issue or what i am doing wrong, ScreenToWorldPoint return (0,18,0) which is my main camera co-ordinate, please help me with this issue.
When you use ScreenToWorldPoint() the third coordinate is the actual distance from the camera ( which in your case is 0) . Try increasing it to gain more "depth" into the world.
Answer by HarshadK · May 16, 2014 at 01:48 PM
Here's a question that discusses the same issue as yours: Camera Screen to world point returns cameras transform position
So basically the reason is that since you are providing 0 for the z there is no depth present.
So if you want to know the world position you have to provide z value for the depth at which you want the point from the world co-ordinate otherwise use raycasting.
Helpful tip: If you store the return value of Camera.main.WorldToScreenPoint()
in a Vector3, despite the z value being irrelevant in screen positioning, the z value required to convert back accurately to world space using Camera.main.ScreenToWorldPoint()
is preserved.
Answer by chethanb · Nov 17, 2020 at 11:34 AM
Find the Y(up axis value) value of the camera and the plane in the world. Find the difference between them and use that as Z value in Camera.main.ScreenToWorldPoint(Vector3(touch.position.x,touch.position.y,Z)));