Raycasting through a custom camera projection matrix

After modifying my main camera’s projection matrix, all methods that use ray casting (OnMouseDown, ScreenPointToRay) begin to fail. From what I can tell, the ray continues to use the camera’s original matrix.

Is there any way to force a ray to use a custom projection matrix?

you can use ur new matrix to transform the point to world space and use a regular ray test

What is the method for this?

you multiply the point by the inverse matrix of your projection matrix, but the viewport point not the screen point , the result will be a point in world space which means the same space for all other objects in the scene , then you can use Physics.Raycast or Physics.Linecast for testing

Ah, I see, thanks! I need to brush up on my linear algebra.

So if I understand correctly, the following should work:

Vector3 mousePosition = camera.ScreenToViewportPoint(Input.mousePosition);
Ray ray = camera.ScreenPointToRay (camera.projectionMatrix.inverse.MultiplyPoint(mousePosition));
bool objectsHit = Physics.Raycast (ray, out hit);
Vector3 hitLocation = hit.point;

Now this gives me funky results, so I messed up somewhere. Could anyone point out my mistake?

Hi,
Has anybody already solved this?
I’m having same problems. I have a custom projection matrix on my camera and raycast doesn’t work. Can anybody help me please?

following - would love to see a solution for this.

public Ray ViewportPointToRay(Vector3 position)
    {
        position.x = (position.x - 0.5f) * 2f;
        position.y = (position.y - 0.5f) * 2f;
        position.z = -1f;

        var viewportToWorldMatrix = (_thisCamera.projectionMatrix * _thisCamera.worldToCameraMatrix).inverse;

        var rayOrigin = viewportToWorldMatrix.MultiplyPoint(position);

        var endPosition = position;
        endPosition.z = 1f;
        var rayEnd = viewportToWorldMatrix.MultiplyPoint(endPosition);

        return new Ray(rayOrigin, rayEnd - rayOrigin);
    }
1 Like