I’m struggling with what looks like a bug in the Unity PhysicsRaycaster.
I have a basic test setup with:
- A
Camera, with aPhysicsRaycasterattached - A cube as a child of the camera, translated/scaled to cover the camera far plane
- An EventSystem
Every frame I’m logging out the results from EventSystem.current.RaycastAll. Obviously I should get a hit on my cube every frame, since it covers the entire camera viewport. What I’m actually seeing is no hits on some frames.
I think I’ve narrowed this down to the calculation of the ray length in the PhysicsRaycaster. This code is copied directly from that class:
ray = eventCamera.ScreenPointToRay(eventPosition);
// compensate far plane distance - see MouseEvents.cs
float projectionDirection = ray.direction.z;
distanceToClipPlane = Mathf.Approximately(0.0f, projectionDirection)
? Mathf.Infinity
: Mathf.Abs((eventCamera.farClipPlane - eventCamera.nearClipPlane) / projectionDirection);
If I replicate it in my test script and draw the results I get this:
When the camera is aligned with the Z axis the length is correct, but when the camera is rotated the length is too short and so the PhysicsRaycaster misses some hits.
Is this a bug in the PhysicsRaycaster component (and if so, is there a convenient workaround)? Or am I just doing something dumb?

