Canvas & collider2D mouse events

So I was using a screen space overlay canvas and everything was working nicely untill I wanted to use a circle collider 2D to catch mouse events. (onmouseenter specifically, so raycasting etc isn’t an option )

The only way I could get a debug log was when I used a screen space camera canvas but then another problem arose, which is when the camera is tilted, the 2d collider was not aimed at the camera so the collider perspective was just wrong.

So right now I’m using a second camera over the main camera with a screen space camera canvas which renders the UI only, but I’d rather it work with the overlay canvas.

Is there any way to get it to work with a overlay canvas?
Was I just doing something wrong or is it normal that 2d colliders don’t catch mouse events in a overlay canvas?

Hi lejean!

I’ve been having a similar problem, and I believe I can help.

View your scene in the Scene view and notice what happens when you toggle Screen Space Overlay to Screen Space Camera. You’ll see that Screen Space Overlay items exist on a very large scale outside of the game camera’s viewport. In order for IPointer events to register, the raycaster (graphic/2d/3d) must physically hit the collider on your ui target. When you go into Overlay mode, the rays coming out of your camera forward to your mouse position no longer hit the ui elements. Also, when you use 2D Box Colliders, they ALWAYS face forward on the absolute xy plane even if your camera is oriented in some other direction and the text/image is facing the camera in screen space.

I’m using 2 Cameras- one camera called Menu Camera for my UI that I’ve linked to a Screen Space Canvas and another camera called Main Camera for rendering my scene. I’m also using 3D Box Colliders and a Physics Raycaster (3D). With this setup, 3D Box Colliders always face the Menu Camera, the ui elements are always in front of the Menu Camera’s raycasts, and IPointer events come through just fine.



So far I haven’t found a way to get raycast events to work in Overlay mode, and I suspect this is because of the spatial positioning relationship between the camera viewport, the raycasts, and the ui elements. In the meantime, this solution should work nicely for you without too much performance overhead because of the Culling Masks.

Hope that helps! It’s been quite a headache for me so far, so I hope this bring some clarity and saves you some trouble.

Cheers