Handling UI Button Clicks Hidden Behind Mesh in PlayCanvas

Hey,

I’m encountering an issue where UI buttons can still be clicked even when they are obscured by a mesh (e.g., a wall or box) with colliders. This behavior feels like a bug or perhaps something I’m missing in my implementation.

Here’s the problem:

  • I want a UI button to only be clickable if it is visible to the camera.
  • Currently, even if the button is behind a wall or hidden from view, clicking in that area triggers the button’s click event.

I’ve created a demo project to illustrate this behavior: Demo Project Link.
In the demo, you’ll notice that clicking the button works even when it’s obstructed by a box.

My Expected Behavior:

When I click on an area hidden by the box, the button should not register a click event. Otherwise, it’s unclear how to distinguish between valid clicks and those that should be ignored.

Attempts So Far:

I’ve tried using raycasts to check for visibility, but since the UI button doesn’t have colliders, I haven’t found a working solution.

Any advice on handling this scenario would be greatly appreciated! Thanks in advance for your help. :blush:

Hi @dimitrisgegas01.

I suggest wrapping your 3D object with 3D UI and enabling useInput for them.

Here is the project and my setup, and you can see this object is preventing the button’s hover:
https://playcanvas.com/project/1277068/overview/ui-behind-3d

Maybe changing the layer of the button to world?

This does not work

Well it was an idea.

If you wrap the button in a collision then you can get the expected behaviour at least for obscuring objects that also have a collision. Here is an example. https://playcanvas.com/project/1276976/overview/disable-button-when-obscured
See the lookingAt script attached to root.

It works by enabling and disabling the the button elements useInput property,

2 Likes

Hey everyone,

Thank you all for your helpful replies and suggestions.

I wrapped the UI button in a rigid body and a collision component with zero resistance. Then, when a click event is triggered, I use a raycast to check which rigid body is actually selected. This ensures that only visible buttons can be clicked, and clicks obstructed by meshes (like walls or boxes) are ignored.

Updated Demo:

For anyone encountering a similar issue in the future, I’ve updated the example project to include this logic:
Demo Project Link

Thanks again for all the insights and support—it made a big difference! :blush:

1 Like

I just enable or disable the button entity