Search Unity

UI Selection from Vive controller raycast

Discussion in 'AR/VR (XR) Discussion' started by freekstorm, Mar 11, 2016.

  1. freekstorm

    freekstorm

    Joined:
    Nov 11, 2010
    Posts:
    86
    Does anyone know how to get a raycast (laser pointer) to register which world space UI component is currently being pointed at.

    I'm not looking for gaze input but for a raycast from the controller?
     
  2. ryleysill

    ryleysill

    Joined:
    Feb 24, 2016
    Posts:
    3
    Hey did you ever manage to find anything out about this?
     
  3. Kiesel

    Kiesel

    Joined:
    Nov 13, 2014
    Posts:
    6
    You could add a Box-Collider to your Canvas-Element, which you can use as raycast target. Then you simply add something like this to your vive controller gameobject.

    void Update () {

    RaycastHit raycastHit;
    GameObject gameObject = null;
    if(Physics.Raycast(transform.position, transform.forward, out raycastHit))
    {
    gameObject = raycastHit.collider.gameObject;

    //Do sth. with the found GameObject here
    }
    }
     
  4. JoeCooper20

    JoeCooper20

    Joined:
    May 6, 2017
    Posts:
    6
    Is that the best way of doing it? In an update loop like that?
     
  5. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    You might want to use FixedUpdate for a raycast? But otherwise, if you don't like Update, you could also use a collider on your controller, and other object - and set it to trigger. Something like:

    private void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.tag == "controller")
    {

    }
    }
     
  6. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    Using the physics raycasting isn't the best way to do this, not to mention it's cumbersome putting colliders all over UI elements, you should making an input module instead. This way you can just attach it to your event sytem. I would look over Unity's source to the standalone input module but in general you would then just need to do the following.

    Code (CSharp):
    1. eventSystem.RaycastAll(eventData, m_RaycastResultCache);
    2. result = FindFirstRaycast(m_RaycastResultCache);
    3. m_RaycastResultCache.Clear();
     
    FlightOfOne likes this.
  7. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    What is eventData? I see PointerEventData but don't understand where that comes from? How do you get PointerEventData from SteamVR?
     
  8. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    PointerEventData is correct. In normal games it's used from a mouse cursor in relation to the camera but it's generic enough for you to populate with whatever you want. Basically you create one and then populate it with the data from what you are using (HMD if it's gaze based or the controller if it's laser pointer based). That way you can just use a graphics raycaster like Unity does by default and don't have to add colliders all over the UI.
     
  9. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Thanks, I believe I am doing something similar to that already and am able to click buttons using IPointerClickHandler .OnPointerClick. All items still require physics colliders, But I still don't understand how to set cursor positions on inputs or drag scrollbars/lists. And I definitely don't understand how to project a GraphicsRaycaster from a VR controllers's tip to the UI plane in world space without using cameras.
     
  10. AccentDave

    AccentDave

    Joined:
    Nov 16, 2015
    Posts:
    43
    Waiting for a resolution to this as I'm having the same issue