ScreenPointToRay effected by rendering approach.

Hey folks, not getting much luck on the answers page so I thought I’d try posting here as well.

Recently setup a scene to render everything to a render texture of a fixed size, and then render it to the screen by texturing it to a plane via an orthographic camera. The rendering works fine, and produces the same results on both the orthographic approach, and the normal camera rendering directly to the screen.

What doesn’t work the same in both cases however is the Camera.ScreenPointToRay() function. Which when not rendering directly from the perspective camera seems to break.

Here is the projected ScreenPointToRay result when rendering directly to the screen from the perspective camera. (red point marks where the screen input was provided).

And here is the projected ray when rendering to the screen via the RenderTexture and orthographic camera. (note although shown from the side view, there is also a similar ray discrepancy when viewed from above.

The line of code directly relating to the raycast is this.

            ray = _cam.ScreenPointToRay(ScreenCoords);
            HasResult = Physics.Raycast(ray, out _RayHitInfo, Mathf.Infinity, Physics.kDefaultRaycastLayers  ~(1 << LayerMask.NameToLayer("Shadow")));

This is on a script attached to the perspective camera and is run when input is detected. And I’ve had it output the ScreenCoords and the cameras aspect values to check, and in both modes they return the same values ( I cant think of any other value on the camera that would affect the raycast, but maybe I’m missing something?)

To see the rest of the code, and test the scene for yourselves, there’s a unity package HERE that you can download.

The test scene has 4 spheres that should cycle colours when clicked or tapped on, and a button to swap between modes. Uses render textures so will only run on pro.

ISSUE RESOLVED:
Managed to resolve the issue by basing raycasts off of viewport coordinates instead of screen coordinates (using Camera.ViewportPointToRay()). Unsure as to why this method works but screen based coordinates don’t seeing as they both provide the same values in both rendering modes. But in case anyone else has this issue, this is how I got around it.

+1 to this still being an issue in Unity 4

Hey, if anyone ends up here and doesn’t get it you should do something like this:

 void Update()
    {
        var pos =mousePosition;

        var viewportPos = new Vector2(pos.x / Screen.width, pos.y / Screen.height);
       
        ray = cam.ViewportPointToRay(viewportPos);
        if (Physics.Raycast(ray, out hit, Mathf.Infinity, layer)) {
            print(hit.collider.name);
        }
    }

The linecast can be a raycast or whatever you want