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. Dismiss Notice

OverlapPoint ignoring the UI

Discussion in 'Scripting' started by Slabada, May 20, 2022.

  1. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    Can OverlapPoint ignore UI elements? instead of passing through?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    What part of the documented behaviour of OverlapPoint is surprising you?

    eg,

    "I have A and B in my scene and the documentation says OverlapPoint should do X.

    But when I try it, I get Y instead."
     
    Slabada likes this.
  3. Slabada

    Slabada

    Joined:
    May 2, 2021
    Posts:
    50
    Sorry, I didn't put it right, can OverlapPoint not ignore UI elements?
    My case is that the control is done using OverlapPoint and when a window appears, OverlapPoint completely ignores the UI and when interacting with the UI, the character moves.
     
    Last edited: May 20, 2022
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Are you talking about Physics2D.OverlapPoint() ??

    What part of the documentation indicates it has anything to do with UI?

    My reading is that it is a Physics2D function concerned with 2D colliders... or are you talking about some other method entirely?
     
  5. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    I am guessing that you are calling PhysiscsOverlapPoint based on raw mouse state. As Kurt-Dekker said OverlapPoint said it has nothing to do with UI. Just like other XOverlap methods and XCast methods you are allowed can query the presence of physics coliders at any point of time at any position of world, it doesn't need to be performed on mouse position.

    You probably need not to perform the OverlapPoint call and the related logic if you click UI. One of the simplest ways for doing that is adding transparent UI rectangle behind rest of the UI, that way when it receives mouse click event you know that you didn't click rest of UI which is on top of it.

    It is probably also possible to do something using the Raycaster infrastructure (the ones that inherit from UnityEngine.EventSystem.BaserRaycaster and are attached to camera and canvas).

    Depending on your needs it might be also possible to use IPointerClickHandler (and related interfaces) attached to objects with physics colliders. If I am not mistaken this approach should also take into account UI layering.