0

Short story: I have a button on a world space canvas with a click event handler. This is inside (in 3d space not parenting) of a 3d collider with it's own click event. 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?

NOTES:

  • 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.
John Stock
  • 1,104
  • 2
  • 9
  • 11
  • Welcome, this is an age-old problem in Unity :/ I don't understand your *exact* problem 100% but please read this essay - https://stackoverflow.com/a/35530316/294884 - let me know if the issue is resolved for you ! – Fattie Dec 23 '20 at 14:19
  • However you get the click event you can probably just ignore the layer the other collider is on? Have you tried to put your parent collider thingy on the `IgnoreRaycasts` layer? Alternatively have a class on your parent collider which forwards a raycast and tries to hit the button .. only if it misses it does it's won stuff, otherwise execute the button – derHugo Dec 23 '20 at 15:02

1 Answers1

0

I've been able to fix this by putting the button's world space canvas on a sorting layer of 1. That way, even if it's behind colliders, it will register first. Nice and clear solution that I was hoping would exist.

John Stock
  • 1,104
  • 2
  • 9
  • 11