0

I'm trying to make a worldspace Canvas and interract it with worlds object (3D raycast).

I have a Canvas with a GraphicRaycaster and UI elements with raycast target on. I found some code on internet about UI raycast and I have tested them to simulate a mouse cursor on my Canvas. (I want an hover effect on my buttons, simulate clicks ...).

GraphicRaycaster canvasRaycaster;
Vector2 screenPoint;

void Update()
{
        List<RaycastResult> results = new List<RaycastResult>();
        PointerEventData eventData = new PointerEventData(EventSystem.current);

        eventData.position = screenPoint;
        canvasRaycaster.Raycast(eventData, results);

        foreach (RaycastResult result in results)
                Debug.Log("Hit " + result.gameObject.name);
}

Nothing happens (no debug.log), I've tried with all values between my screen's height and width and between 0 and 1 (x and y) stored in Vector2 screenPoint.

  • What happens if you use `eventData.position = Input.mousePosition;` instead? – Ruzihm Sep 19 '19 at 17:42
  • I can't use eventData.position = Input.mousePosition; cause I'm in VR so I don't have a mouse, just controllers but i'll try to see if anything happens. Is there special colliders for UI ? "Raycast target : true" on UI element are not enough ? – Guilhem Trauchessec Sep 19 '19 at 19:34
  • Wait, why do you need to do this? UGUI objects already work just fine with VR. Buttons click, text fields select, and so on by default automatically for you, even for world space canvases. – Draco18s no longer trusts SE Sep 19 '19 at 21:15
  • Ok so please how can I have a raycast from my controller to a worldspace canvas and interract with it ? – Guilhem Trauchessec Sep 19 '19 at 21:23
  • @Draco18s actually that's tricky ... it works automatically **if** the according SDK for the VR target device provides the according EventSystem/PointerModule for handling things like PointerEnter, PointerDown etc .. if this is not the case you sometimes have to implement your own which can get quite complex. – derHugo Sep 20 '19 at 04:29
  • It depends a lot on which VR device you are using .. ? An alternative might be using something like [this](https://stackoverflow.com/a/56645337/7111561) where I once used normal 3D colliders to interact with UI via a "LaserPointer" that uses a raycast in order to fire the according Pointer events so Unity UI can handle them as if it was the mouse – derHugo Sep 20 '19 at 04:38
  • `eventData.position` has to be passed in screen pixel coordinates. in VR `Input.mousePosition` will always be `0,0` but you would have to pass in the center of your screen not `Vector2.zero` which seems to be the case currently (I don't see if and where `screenPoint` is set so it would have the default value). You could try and use `eventData.position = new Vector2(Screen.width, Screen.height) / 2f;` – derHugo Sep 20 '19 at 04:50
  • @derHugo Fair enough. But every VR device I've ever gotten my hands on has worked out of the box. – Draco18s no longer trusts SE Sep 20 '19 at 13:11
  • @Draco18s then you are a lucky guy ;) – derHugo Sep 20 '19 at 13:22
  • @derHugo Oculus DK1, Oculus DK2, Hololens 1, and several phones that were used in Daydream setups or comparable Oculus widget (I forget what it's called). Vive also works out of the box, last I checked. That covers pretty much all the VR hardware on the market. – Draco18s no longer trusts SE Sep 20 '19 at 13:44
  • Thank you guys! I found this vidéo : [01 Unity SteamVR Canvas Pointer](https://www.youtube.com/watch?v=3mRI1hu9Y3w), he explains everything and it works! – Guilhem Trauchessec Sep 20 '19 at 17:57

0 Answers0