0

I need some help with raycasting UI with Daydream. As there are only a demo for gameObjects (cubes), I want to know I can handle UI elements. The UI elements should react as usual, I mean highlighted and so on. The posts in How to use Graphic Raycaster with WorldSpace UI? were helpful, but completely.

I used DrawRay to see where my pointer is actually going through and it works good. But no log message is created.

 void Update()
  {
      Quaternion ori = GvrController.Orientation;
      Vector3 vector = ori * Vector3.forward;
      Debug.DrawRay(transform.position, vector * 100000, Color.green);
      PointerEventData pointerData = new PointerEventData(null);
      pointerData.position = vector;
      Debug.Log(vector);
      List<RaycastResult> results = new List<RaycastResult>();
      EventSystem.current.RaycastAll(pointerData, results);
      if (results.Count > 0)
      {
          Debug.Log(results[0]);
      }
  }

Canvas (World Space) and Button is created as useal and not modified.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
fmielke
  • 70
  • 1
  • 11

3 Answers3

0

In your case I see two problems :

  • I think you should use PointerEventData pointerData = new PointerEventData(EventSystem.current); insted of PointerEventData pointerData = new PointerEventData(null);
  • Also PointerEventData.position is a Vector2 and not a Vector3 : you should try pointerData.position = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);

As a side note you can use graphicRaycaster.Raycast(pointerData, results); where graphicRaycaster is public and you assign it the GraphicRaycaster component of you Canvas.

Kardux
  • 2,117
  • 1
  • 18
  • 20
  • Thanks @Kardux for the tips. But unfortenately it does not work. 1. How should the thing with the **Vector2** and the `Screen` work? I don't need screen sizes, but orientation from the controller. `vector` output is (-0.5, 0.0, 0.9) and `pointerData.position` is (-0.5, 0.0) - can that work? 2. I added the **GraphicRaycaster** component to my parent Canvas and used `public GraphicRaycaster graphicRaycaster`, but not log entry at all... any other idea? Do i need a GUICamera? – fmielke Nov 02 '16 at 15:35
  • @fmielke My bad about the **Vector2** thing including Screen sizes : in my case I always use the middle of the "screen" to raycast against UI (this way the user simply look at the UI to activate it). By giving a **Vector2** a **Vector3** value you simply don't consider the _z_ value (this is why you get (-0.5, 0.0)). In your case I would raycast normally using `Physics.Raycast` against a "ghost wall" placed where your UI is and then use `Camera.WorldToScreenPoint` to get the screen _x_ and _y_ coordinate to set `pointerData.position`. – Kardux Nov 02 '16 at 15:49
  • I actually "just" need the gameObject behaviour from `RaycastHit hitInfo; if (Physics.Raycast(Vector3.zero, vector, out hitInfo)) { Debug.Log("HIT a gameObject"); }` But only for UI :) – fmielke Nov 02 '16 at 17:08
0

How about this useful method:

Vector3 CurrentRaycastResultWorldPosition = GvrPointerInputModule.Pointer.CurrentRaycastResult.worldPosition;         
-1

According your question raycasting world space UI ,if you are using google cardbard or daydream than you should try to attach script gvrgraphicphysics raycaster to UI canvas.

manju
  • 1
  • 3