Inconsistency Result of ScreenPointToRay between UnityEditor and Real Device
Hi, I've suffered from a wired exception. this part of test code runs out inconsistent results
Transform camera_trans = GameManagers.CameraMgr.SceneCamera;
if (camera_trans == null) return "";
Camera camera = camera_trans.GetComponent<Camera>();
Vector3 screen_pos = Vector3.zero;
screen_pos.x = 960.0f;
screen_pos.y = 540.0f;
Ray touchRay = camera.ScreenPointToRay(screen_pos);
Debugger.Log("CameraPos:" + camera_trans.position.ToString());
Debugger.Log("RayCastTo3DObj:" + screen_pos.ToString() + " " + touchRay.ToString());
On UnityEditor, this will print
0:4:7.448-277: CameraPos:(-25.4, 58.3, -343.6)
0:4:7.448-277: RayCastTo3DObj:(960.0, 540.0, 0.0) Origin: (-25.4, 58.3, -343.5), Dir: (0.0, -0.3, 0.9)
On RealDevice(an un-exploded Samsung Galaxy 3), adb logs
0:4:7.448-277: CameraPos:(-25.4, 58.3, -343.6)
0:4:7.448-277: RayCastTo3DObj:(960.0, 540.0, 0.0) Origin: (-25.4, 58.3, -343.5), Dir: (0.4, -0.1, 0.9)
the resolution is 1920*1080 (double checked on Real Device) Is there any other factors of camera which may affect the result of the ray, when I send in same inputs?
BTW: Unity version 5.4.0b20
if you're using a perspective camera, this result is expected. make it orthographic and it's the same result.
it's because of the different aspect ratios the editor and the device are using. since the camera view in perspective mode is like a fan, the ray direction is slightly different
First of all, thx 4 ur answer. However, perspective camera is a must in my project.
In fact, I'm working on click-select. The ray calculated on UnityEditor leads to correct result(means I can click on the screen and the ray collides the exact gameobject I wanna select), but the results on real device leads to a wired performance because of the inconsistent ray direction. Is there any approach to make it work correctly on real device ? $$anonymous$$aybe I need some more coordinator transition before I send the screen pos into camera?
I can't see you using any touch or mouse position, is that in a different script?
Fixed! It's still a resolution issue.
Our guys on 3D/Render redefined the resolution of rendering buffer without adjusting the resolution(in fact, game resolution is 1920*1080 but 3D resolution is auto configured by device 1344*756) So the screen pos is not approciate to the 3D camera resolution. After adapting the screen pos to camera resolution, everything goes fine.
Your answer
![](https://koobas.hobune.stream/wayback/20220612104700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Screen Space - Camera makes bullets fly in wrong directions 1 Answer
converto text to texture2D 0 Answers
Where is the screen within a camera's frustum? 0 Answers
Frustrating problem with UI canvas (lines of canvas flicker around the edges of screen in game!!!) 5 Answers
UI GameObjects showing in scene view but not game view? 0 Answers