Search Unity

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

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

  1. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    65
    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
  2. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    65
    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:
    65
    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