Get "Cursor Position" on VR Enabled Canvas

Does anyone know of a great way to get the “cursor position” on a Canvas when using the XR Interaction Toolkit?

Use Case

  • I want to know where the player is pointing on the canvas so that I can drag an icon to where they are pointing.
  • As they continue to point along the canvas, I’d like to make the icon follow.

Context

  • I’m using an Event Trigger to detect when the Ray Interactor enters the Canvas. That is working.
  • I can’t figure out the best way to determine where the Ray Interactor intersected with the Canvas.
  • I believe running my own Raycast would work and I’m going to try that next.
  • Running another Raycast seems redundant, so I’m wondering if anyone is aware of a better way.

Update: TryGetCurrent3DRaycastHit on the XR Ray Interactor can be used to run a Raycast, but it doesn’t seem to detect UI elements by default.

Update: a related method (TryGetCurrentUIRaycastResult) on the XR Ray Interactor does work for UI elements!

It also seems like TryGetHitInfo is the more generic function to call. If I’m reading it correctly, it will return the position, normal, and more for 3D and UI raycasts.

For anyone interested, here’s a hacky solution.

public class TestPointer : MonoBehaviour
{
    public XRRayInteractor rayInteractor;
    public GameObject cursor;

    private void Update()
    {
        RaycastResult res;
        if (rayInteractor.TryGetCurrentUIRaycastResult(out res))
        {
            cursor.transform.position = res.worldPosition;
        }
    }
}

The cursor GameObject is an object in the scene that I’m moving to the position of the UI Raycast Hit.

I’ll proceed with this, but I’m still wondering: does anyone know of a better way? For example, I feel like some part of the UI system is probably tracking this already but I just don’t know the right part to look for.

1 Like

Hi

I am looking to do a similar thing you describe in your initial post. When trying your code, however, it does not seem to recognise “XRRayInteractor” as a variable I can set in the following line.

public XRRayInteractor rayInteractor

I am very new to Unity so guessing I am probably missing something quite obvious but wondered if you could help out?

Thanks

Add it to the hand controllers (or gaze controllers?) I’ve found it here:

(47) Unity VR Game Basics - PART 5 - XR Ray Interactor in 10 Minutes - YouTube

I’m trying to do something similar. But I’m stuck on the first part itself. How did you get event triggers for UI Canvas using XRRayInteractor. When I try to use SelectEntered Events, it only runs for GameObjects other than Canvas. Please help me.