Search Unity

Define a new Ray Caster to interact with UI elements

Discussion in 'EditorXR' started by korut94, Dec 3, 2018.

  1. korut94

    korut94

    Joined:
    Sep 2, 2013
    Posts:
    1
    Hi all,
    for my university project I want move a pointer over the Tool's canvas and instead to use the rays projected by the controllers I would like using the front face of my pointer as new "ray origin" to interact with my UI elements. Is it possible? I don't understand how to work with the Graphic Raycaster. Thanks in advance.
     
  2. amirebrahimi_unity

    amirebrahimi_unity

    Joined:
    Aug 12, 2015
    Posts:
    400
    Hi @korut94, I'm having a hard time understanding what you are trying to do? Is it that you want to move a virtual pointer on a surface by using your raycasts? Could you illustrate what you are looking to do somehow?
     
  3. Matt-HitIQ

    Matt-HitIQ

    Joined:
    Feb 15, 2019
    Posts:
    7
    @amirebrahimi_unity I am actually looking to do this using Unity XR + openVR libraries so I can interact with Unity UI if the laser pointer is hitting it. At this stage I am thinking
    • I add a LineRenderer component to the controller
    • At the same time Raycast from my controller
    • If the raycast is intercepted by UI, invoke the appropriate behaviour (OnMouseOver?, OnFocus?)
    • After the user clicks the trigger, invoke the UI element somehow
    I was hoping to find a recent XR example of this somewhere in the docs?

    I am looking to make this for Oculus Rift and Vive controllers

    Cheers
     
    Last edited: Feb 24, 2019
  4. amirebrahimi_unity

    amirebrahimi_unity

    Joined:
    Aug 12, 2015
    Posts:
    400
    Deleted User likes this.
  5. superjayman

    superjayman

    Joined:
    May 31, 2013
    Posts:
    185
    Can you please give code example to actually use these functions eg. simple world space laser pointer.
     
  6. Optrix

    Optrix

    Joined:
    May 30, 2017
    Posts:
    17
    If you have a world-space UI (which I'm guessing you do), you can actually put 3D collision objects on your UI objects (ie. you can throw a BoxCollider on a button).

    This way you can interact with UI elements directly from Physics.Raycast.


    If you want to switch between a Physics.Raycast and a GraphicsRaycaster.Raycast though, there's an issue.

    Everything is pretty straight forward except for mapping between world space and UI space. You can throw a BoxCollider on your Canvas without too much trouble and Physics.Raycast can strike that.

    The tricky bit is working out the UI-space location of that world-space impact point. To be honest, I haven't been able to figure out how that is done. Physics.Raycast returns the world position of your impact point, which is fine - but there's no function to convert between world-space coordinates and UI-space coordinates so that you can use GraphicsRaycaster.Raycast.

    One possible trick that I haven't tried is to use a UV-mapped plane as your collision mesh. Then in theory you can use the UV coordinates of the impact position (found in the HitInfo struct you can pass to Physics.Raycast) and multiply them by the width and height of your UI in order to simulate a mouse-hover.
     
  7. wechat_os_Qy08D70r_Ee8Gx9_4iNwlJqZU

    wechat_os_Qy08D70r_Ee8Gx9_4iNwlJqZU

    Joined:
    Mar 30, 2019
    Posts:
    4
    Have you solved this problem?
     
  8. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    992