Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question World space button click event inside of a 3d collider

Discussion in 'UGUI & TextMesh Pro' started by Ziplock9000, Dec 23, 2020.

  1. Ziplock9000


    Jan 26, 2016
    Short story:
    I have a button on a world space canvas with a click event handler. This is inside of a 3d collider with it's own click event. (When I say inside I mean withing 3d space, not parenting)
    The collider always gets the click event as expected as it's nearer the camera.
    I want the button to get the event.

    Long Story:
    I have a person mesh with a collider. You can click on them and the OnPointerClick triggers to do something.
    I have a button that sits in a world space canvas which itself is located just above the mesh and is pointed towards the orthographic camera. You click on the coin and the OnClick event triggers to do something else.
    Both events work as expected until the coin is inside the mesh's collider (which it is a lot of the time). At which point it's ONLY the mesh collider's OnPointerClick event triggers, not the button.
    However, I ALWAYS want the button to take priority over the collider, and any other collider. This is easier when the canvas is screen space, but it's not (with reason).

    How do I do this?

    - The button never gets the onclick event, so any filtering on the containing collider won't help
    - I've fiddled with the world space canvas and camera ray filtering settings to no effect.
    - The coin has to be a world space canvas for automatic tracking and because I use text too
    - IsPointerOverGameObject doesn't help as it's true for any collider, not just UI elements. Not to mention it wont stop the collider consuming the click anyway. A custom version of this that works via a layer wont help either, because again the OnPointerClick on the collider GO stil consumes the click.
    - I don't want either event to have to do any event filtering & passing on if possible, they should be atomic. Any filtering should be via setting properties in objects and inherent functionality of Unity *if possible*
    - Just to reiterate, writing a function to find all objects in the ray and then selecting ones that are on the UI layer first wont help. Because that does not change the fact that the collider still is the only thing that gets the event, which then you'd have to manually propagate down to the button.. which I don't want to do.
    Last edited: Dec 23, 2020