Hello everyone!
I’m currently working on a 2D game where multiple game objects are stacked on top of each other. Each game object has a sprite renderer, a collider2d and a script attached. In the script, I use the methods OnMouseDown, OnMouseDrag and OnMouseUp to achieve a drag and drop functionality.
The problem is, when there are multiple sprites stacked, the OnMouseDown method seems to choose a random game object instead of choosing the one that is visible on top. It does completely ignore variables like sprite layer sorting order.
I found a solution using google and the unity site. Instead of using the OnMouseX methods on every game object, I created another game object with a “controller” script attached. This script uses various methods, inlcuding raycasts, to find the game object on top (via tag and sprite sorting order). The script then uses custom methods to move the target game object. So far so good.
Does anybody know if the Unity team does plan to give some more functionality to the OnMouseDown method in the near future? Maybe in Unity 5? If not, is my solution the best thing I can do?