Denis Kulikov
Denis Kulikov

Reputation: 71

How can Raycast interact with Unity's Canvas UI elements?

I created a Raycast that shoots from an object(not main camera) and I want it to hit UI elements. Example below. Example

UI outlined in orange, big grey plane is a test object, red head is placed upon user's head and green is DrawRay of my raycast attached to an eye. I am trying to make it so user looks at a UI button and raycast can hit that button.

I've been playing around with Graphic raycast but nothing seems to work. Grey plane is still being hit by the raycast. I tried playing with OnPointerEnter but the thing is that my eye raycast is not a mouse/finger touch and is a second pointer. Any ideas how to make it work with either isPointerOverGameObject or Graphic Raycaster or any other method?? Or how to create a second pointer that will be the eye raycast? Current code below.

private void FixedUpdate()
{
    GraphicRaycaster gr = this.GetComponent<GraphicRaycaster>();

    PointerEventData pointerData = new PointerEventData(EventSystem.current);

    pointerData.position = Input.mousePosition;

    List<RaycastResult> results = new List<RaycastResult>();
    gr.Raycast(pointerData, results);
    //EventSystem.current.RaycastAll(pointerData, results);
    if (results.Count > 0)
    {
        if (results[0].gameObject.tag == "Test tag")
        {
            Debug.Log("test");
        }
    }




    //----- Failed Graphic raycast experiments above; working RaycastHit below --------


    RaycastHit hit;


        Debug.DrawRay(transform.position, transform.forward * -10000f, Color.green);

        if (Physics.Raycast(transform.position, transform.forward * -1, out hit))
        {

            Debug.Log(hit.transform.name);
        }



}

Upvotes: 3

Views: 7834

Answers (1)

Krzysztof Bociurko
Krzysztof Bociurko

Reputation: 4662

This is actually much more complicated if you want to do proper interaction with the canvas elements.

I suggest you do not try to roll out the solution yourself, but look at some already made solutions. Most VR toolkits have their own implementations, you could look into VRTK's example scene 007 - Interactions. You don't have to use VR to use it - just use the fallback SDK.

For more details on how this works, I might point you to the article from Oculus: Unity Sample Framework (a bit out of date).

Upvotes: 1

Related Questions