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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can UI elements block physics raycast?

Discussion in 'UGUI & TextMesh Pro' started by Nubeh, May 20, 2015.

  1. Nubeh

    Nubeh

    Joined:
    Oct 7, 2012
    Posts:
    47
    Hi there,
    I want to listen for touch end on mobile device in order to do a raycast at a certain distance and check some objects interaction. I have a virtual joystick as well and the problem now is that when the user leave the joystick a raycast is triggered by TouchPhase.Ended but I don't want that because it's not a touch meant for interaction.

    I tried to use:

    if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended){
    if(EventSystemManager.currentSystem.IsPointerOverGameObject(Input.GetTouch(0).fingerId){
    ...do nothing
    }


    but it doesn't work. I tried adding CanvasGroups to my joysticks (I have 2) that block raycast but that is not related to physics raycast. Does someone know a good solution for that?

    Thanks
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  3. Nubeh

    Nubeh

    Joined:
    Oct 7, 2012
    Posts:
    47

    Hey, thanks for your reply. I saw that video already, the problem with that is that I need to have control over the distance of the ray as well. Also I prefer to raycast against a specific layer rather than having to make sure that each intractable object has a script attached that inherits the clickable interface.
    Is there really no way to do it with the standard physics raycast?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you are desperate you could add a regular physics collider to the UI element. Don't know how well it will work.
     
  5. Nubeh

    Nubeh

    Joined:
    Oct 7, 2012
    Posts:
    47
    In the end I implemented the IPointerClickHandler interface. That said, is there a way to prevent a player to tap on an object from a great distance by limiting the Physics Raycaster ray distance?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Check the distance inside of the IPointerClick method?