Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

EventSystem & Physic2DRaycaster on overlapping Collider2D

Discussion in 'UGUI & TextMesh Pro' started by downstroypvp, Apr 11, 2020.

  1. downstroypvp

    downstroypvp

    Joined:
    May 30, 2016
    Posts:
    35
    Hi!

    I am having a trouble understanding what is happening with my clicks on my 2D colliders. I have a background sprite that covers the entirety of the screen, it is on the "background" layerMask, and Z position 0. It has a collider and IPointerClick handler, and clicks are detected on it.

    I have enemies and tower that are on the "object" layerMask, same Z position as the background (0), with collider, all are "inside", the big background collider. All the objects implements the IPointerClick handler and clicks are detected on them, and the raycast doesn't hit the background collider.

    Now I have another "spot" layerMask, with objects inside being at Z position 0, with collider, and implementing the IPointerClick interface too. They also are "inside" the background collider.

    Now when I click on those objects, the clicks is detected on the background collider instead of these colliders, contrary to the enemies and tower inside the "object" layerMask. If I disable the background collider, then my clicks are detected on the "spot" layerMask objects.

    As I have read, The physics2DRaycaster returns colliders in no particular order. My camera is at the -10 Z (orthographic), so the raycast doesn't start inside a collider, and my Physics2DRaycaster is set to interact with those 3 layerMasks, with a maxRayIntersection of 0.

    1°) Does The Physics2DRaycaster returns all the collider it hits, even if they are overlapping in the same Z position? It appears that it only randomly select one to return.
    2°) I tried to change the maxRayIntersection to 10, it doesn't have any effect on the clicks interaction
    3°) I also tried to set the objects that are in the "spot" layerMask to be closer (Z wise) of my camera, but it didn't change the click detection.
    4°) I tried to change the objects of the "spot" layerMask to the "object" layerMask, without any effects on the click detection, the background is still hit first.

    I have read those answers, and tried all those methods, to noavail (I didn't try with 3D colliders though, and I won't as it's very hacky):
    - https://answers.unity.com/questions...s-that-over.html?page=2&pageSize=5&sort=votes
    - https://answers.unity.com/questions/748138/order-of-2d-colliders.html
    -https://answers.unity.com/questions/598492/how-do-you-set-an-order-for-2d-colliders-that-over.html
    -https://forum.unity.com/threads/c-o...st-sortingorder-is-not-selected-first.244627/

    If anyone has an explanation, or a solution to this problem I would gladly hear it.

    Thank you all!
     
  2. MrPaparoz

    MrPaparoz

    Joined:
    Apr 14, 2018
    Posts:
    156
    I don't think you should use IPointer handlers on gameObjects. Its primary use is UI.

    Secondly, I am using Physics.Raycast on a BoxCollider with (x, y, 0) scales, so it doesn't have any depth. It works as intended. No need to deal with 2D physics there.

    So, add a box collider via inspector or via code.

    Declare your layer mask (Let's say you are ignoring 10th layer which is "object" layer of yours).
    Code (CSharp):
    1.  
    2. private LayerMask ignoredLayers =~ (1 << 9); // "~" symbol here takes opposite of what you declare. It excludes anything that isn't 1 to 9.
    3. private Camera _camera;
    4.  
    5. private void OnEnable(){
    6.     _camera = Camera.main;
    7. }
    8.  
    9. //raycasting
    10. private void RayCasting(){
    11.     var ray = _camera.ScreenPointToRay(Input.mousePosition);
    12.     if(Physics.Raycast(ray, out var raycastHit, ignoredLayers){
    13.           var hitPosition = raycastHit.point;
    14.           //do what you want with that position.
    15.     }
    16. }
    17.  
    Hope that helps.
     
  3. downstroypvp

    downstroypvp

    Joined:
    May 30, 2016
    Posts:
    35
    Thanks, but I think I need to clarify something. I am making a 2D game, so for now all my interaction with UI are done with the GraphicRaycaster and Image. BUT some of my interactable objects (enemies, towers, spots etc) are actually sprite because they are part of the 2D game.

    Before trying to use Physics2DRaycaster, I was using GraphicRaycaster, with a canvas attached to each of those object so they can get the clicks, but it was not very convenient for coordinate convertion of sprite position and image canvas position, So I wanted to switch to an all 2DRaycaster for in-game objects. This is where I got the above weird behavior of the Graphic2DRaycaster not returning the right 2D colliders for a reason I can't really understand.

    I have no plan on using anything related to 3D, be it Raycast or colliders.
     
    Last edited: Apr 14, 2020
  4. downstroypvp

    downstroypvp

    Joined:
    May 30, 2016
    Posts:
    35
    Allright I got it. On the particular objects that were not getting the touches, the SpriteRenderes were not actually directly inside the gameobject which has the Collider, and that seems to be the problem. I just moved the sprite Renderers on the same gameobjects the collider and the OnTriggerClick script were and the clicks are rightly detected on the object and not on the background anymore.
     
    ZemGameZ and huulong like this.
  5. ZemGameZ

    ZemGameZ

    Joined:
    Sep 29, 2020
    Posts:
    2
    Thanks for this!! You are spot on!
    I was just about to toss my pc into a small campfire I made in the yard...
    You saved it's life! (and my time :):))