Search Unity

Is OnMouse.. fundamentally broken on 2D overlapping object colliders?

Discussion in '2D' started by RidgeWare, Mar 18, 2019.

Thread Status:
Not open for further replies.
  1. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    All my sprites are rendering perfectly in correct order using the layer/sorting system on the same z-axis - in accordance with Unity's own recommended approach to 2D rendering - and OnMouseEnter works perfectly on each of them individually when they're separated.

    But when they are close together & overlap, OnMouseEnter/Over is not able to determine the correct collider to use. So I often get rear objects running the OnMouseEnter code incorrectly.

    Is there an obvious solution to this that I'm missing? It seems strange that there is no built in function for a 2D-OnMouse event to look at multiple colliders on the same Z-axis & then simply check the Y position to determine which is the correct gameobject.

    Surely detecting overlapping fore/rear sprites with the pointer correctly is a fundamental necessity in 2D game development? Is there no built-in way to do this?
     
    Last edited: Mar 20, 2019
    kcfos likes this.
  2. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    Am I going to have to end up basically firing a RayCastAll from the pointer position every frame in the update - which logs multiple colliders and registers the one with the lowest Y position?

    And then, if that object is set to do something if the mouse is confirmed as over the collider it executes the code?

    Esentially, remaking the whole MouseEnter/Over/Exit process from scratch and having it permanently switched on for multiple collider sorting.

    If anyone's got a better idea, I'm all ears!
     
  3. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    Well, I implemented the once-a-frame RayCastAll+Iteration method to sort the colliders and it does the job. Although I'm a bit concerned about the extra overhead, but since I'm not coming across any other solutions I guess that'll have to suffice.

    I'm wondering whether the whole 2D sprite renderer/collider concept itself needs to be reworked from the ground up by the Unity team, so it can reconcile itself with the pointer position in a much more natural way. The engine always knows where the pointer position is, and the engine always knows whether a sprite should be at the front or not: Putting those two bits of information together for an instant decision on whether the pointer is over the front sprite sounds reasonable surely?

    Is it worth me putting in a feature request for this? Or even raising this as a bug? Since OnMouse is effectively unusable for 2D game development with overlapping sprites. Seems to be a broken feature as far as I can work out.
     
    Last edited: Mar 20, 2019
    kcfos likes this.
  4. kcfos

    kcfos

    Joined:
    May 20, 2018
    Posts:
    16
    I have found OnPointerEnter to be a working alternative, for anyone else having trouble with OnMouseEnter
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Also note that UI events are nothing to do with the 2D system, they are a UI feature implemented by those teams so best posting about it on the UI forums here.
     
  6. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    I was told by another moderator to post it in the Physics folder, which I did here:

    https://forum.unity.com/threads/bug...axis-trigger-incorrectly.647725/#post-4339663

    But it never got a response from anyone on the Unity team. Not even an acknowledgement.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    I can tell you definitively that neither the 2D team nor anyone on the 2D/3D physics teams are involved in this. It's a UI Event thing and is dealt with by those teams. It might use physics but in the same way you do; it's a user of physics, not designed/implemented by the physics team.

    The reason I am telling you this is because it's unlikely that anyone from the UI teams monitor the 2D or Physics forums.

    It should be obvious though that physics knows nothing about render sorting so whatever query the UI events is doing, it'll not return results by any render order. Beyond that I'm not sure what else to say.

    If it were me, I'd use Physics2D.OverlapPoint to return all 2D colliders overlapping that world-point however I'll say again, neither 2D or 3D physics knows about anything to do with rendering i.e. sorted order etc.
     
  8. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    67
    Wherever the correct location should be for that Feedback/Bugfix request I linked to above (which I also formally submitted), it's not great that I've never had a response to it.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    If you suspect a bug then post a bug report and then you'll get a response. Free support is offered as/when Unity devs have time to go through forums. Posting in the wrong forum isn't going to get the attention from the correct Unity devs which is why you did get a reponse from me (2D Physics Dev) directing you to get the info you need.
     
    Last edited: Jan 28, 2021
  10. dreamer66

    dreamer66

    Joined:
    Jul 15, 2017
    Posts:
    24
    it's prob too late for this topic, but I never found a solution either.
    But what I did to make it work is enable/disable the colliders. Everytime I evaluate an OnMouseEnter for example, I will disable any colliders that could be overlapping with it, leaving it the only collider under the mouse, and on OnMouseExit, I will re-enable all colliders. So if there's overlapping situations, this will make sure that one gets picked and gets sticked to it until it is no longer casted by the cursor.
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    The physics system doesn't know about render order.

    This is nothing to do with any 2D features and especially not 2D physics. These callbacks are just performing a regular query that you too can call; it is not implemented by any 2D code. The same code runs for 3D colliders too.

    Unfortunately, necroing this post here on the 2D forum won't get you any closer to your goal.

    You should post on the UI forums.
     
Thread Status:
Not open for further replies.