OnMouseDown smarter priorities

Hello Unity Forum!

The OnMouseDown Callback (and similar mouse events) can not only be used by UI-Elements, but also by game objects with a collider. However, if multiple colliders overlap with the mouse position when the mouse button is pressed, only one game object gets OnMouseDown callbacks called.

In a 2D Game Setting, Colliders/GameObjects are considered by Z-axis order and when it comes to a draw, it is seemingly random which object gets the call. (Creation/Edit order of the game objects in the editor seems to have something to do with it)

However, in my opinion, a very big use case that a lot of developers will intuitively go for with OnMouseDown is giving game objects with sprite renderers a 2DCollider to make them clickable. In this specific scenario Z-axis order will always result in draws (z typically being 0). However, there already exists a very explicit order on the game objects: the rendering order.

My request/proposal is:

Extend the usage of GetRayIntersection that currently determines which game object receives the callback on mouse clicks, so that draws (that are currently resolved randomly anyway) favor game objects with sprite renderers, with them being ordered like with rendering (layer + order in layer). Basically, in a 2D Game a Mouse Click should hit the frontmost sprite, since users typically click what they can see.

Thank you for your Time!

1 Like

I’d like to echo this, even within the UI elements framework itself it currently appears impossible to layer objects on top of eachother as the top object will always “consume” the click. Additionally, supporting things like alpha-clickthrough or even simply replicating the “raycast target” bool in UGUI seems basically impossible as it stands currently.

Cheers.

2 Likes

i am also trying to do this where I use a mouse to “shoot” at the screen using
Mouse.current.position.ReadValue() and the camera, but only if it doesn’t hit any UI… but right now it always triggers both

1 Like