Search Unity

input event consumed by image raycast target that is “under” a sprite with a 2D collider

Discussion in '2D' started by downstroypvp, Nov 9, 2019.

  1. downstroypvp

    downstroypvp

    Joined:
    May 30, 2016
    Posts:
    40
    Hello everyone.

    I am having a hard time getting my UI to work.

    Basically I have a canvas with a UI image that is the size of the screen. This image has raycast enabled and if effectively receiving inputs.

    Not in the canvas I have a sprite that have a 2D collider attached to it. No matter what I try, the UI image is always consuming the input event when I click on the sprite.

    When I disable the image, the sprite gets the events, so the setup looks right (camera with Physic2D raycaster for the sprite, layerMask set to the layer of the sprite, canvas with Graphic raycaster for the image, no blocking object, no mask).

    What do I need to do so that the sprite gets the input event BEFORE the image and consume it?
     
  2. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    Sounds like you are using a graphics raycaster, instead of a physics2d raycaster for 2d colliders?

    Anyways checked the https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.GraphicRaycaster.Raycast.html, you can always create a list of raycastresults and just check if it is in the list apparently

    Code (CSharp):
    1.             //Create a list of Raycast Results
    2.             List<RaycastResult> results = new List<RaycastResult>();
    3.  
    4.             //Raycast using the Graphics Raycaster and mouse click position
    5.             m_Raycaster.Raycast(m_PointerEventData, results);
    6.  
    7.             //For every result returned, output the name of the GameObject on the Canvas hit by the Ray
    8.             foreach (RaycastResult result in results)
    9.             {
    10.                 Debug.Log("Hit " + result.gameObject.name);
    11.             }
     
    Last edited: Nov 10, 2019