Search Unity

Oculus Go - UI interaction with the controller

Discussion in 'VR' started by Arsandis, Dec 11, 2018.

  1. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    20
    Hi guys,
    I am casting a ray fro the controller and see if there's an object in front of it and interact with it. I have used the following script for that:

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class PlayerPointer : MonoBehaviour
    10.  
    11. {
    12.  
    13.  
    14.  
    15. //Returns whatever object is infront of the controller
    16.  
    17. private GameObject pointerOver;
    18.  
    19. [SerializeField]
    20.  
    21. //Is the object that is currently intractable
    22.  
    23. private PropBase selectedObject;
    24.  
    25. //Is the object currently stored in hand, ready to throw.
    26.  
    27. [SerializeField]
    28.  
    29. private PickUp inHand;
    30.  
    31.  
    32.  
    33. //This is a refrance to the object we want the pointer to be cast from.
    34.  
    35. [SerializeField]
    36.  
    37. public Transform controllerRef;
    38.  
    39. //This is where we want object we are holding to appear
    40.  
    41. [SerializeField]
    42.  
    43. private Transform holdingRef;
    44.  
    45. //The amount of force we want to throw objects from our hand with.
    46.  
    47. [SerializeField]
    48.  
    49. [Range(2, 50)]
    50.  
    51. private float throwForce = 30;
    52.  
    53.  
    54.  
    55. //The script that handles the visuals to show what object is selected
    56.  
    57. [SerializeField]
    58.  
    59. private HighlightObject selectVisual;
    60.  
    61. private LineRenderer line;
    62.  
    63.  
    64.  
    65. void Start()
    66.  
    67. {
    68.  
    69. line = GetComponent<LineRenderer>();
    70.  
    71. }
    72.  
    73.  
    74.  
    75. void Update()
    76.  
    77. {
    78.  
    79. //If a object is currently being held I don't want to select another object until it is thrown.
    80.  
    81. if (inHand == null)
    82.  
    83. {
    84.  
    85. WorldPointer();
    86.  
    87. }
    88.  
    89. else
    90.  
    91. {
    92.  
    93. line.SetPosition(0, controllerRef.position);
    94.  
    95. line.SetPosition(1, controllerRef.position);
    96.  
    97. pointerOver = null;
    98.  
    99. }
    100.  
    101. //This function handles how you intract with selected objects
    102.  
    103. Intract();
    104.  
    105. }
    106.  
    107.  
    108.  
    109. //This function handles shooting a raycast into the world from the controller to see what can be intracted with.
    110.  
    111. void WorldPointer()
    112.  
    113. {
    114.  
    115. //We set the line visual to start from the controller.
    116.  
    117. line.SetPosition(0, controllerRef.position);
    118.  
    119.  
    120.  
    121. RaycastHit hit;
    122.  
    123. //We reset the pointer so things don't stay selected when we are pointing at nothing.
    124.  
    125. pointerOver = null;
    126.  
    127.  
    128.  
    129. //This sends a line from the controller directly ahead of it, it returns true if it hits something. Using the RaycastHit we can then get information back.
    130.  
    131. if (Physics.Raycast(controllerRef.position, controllerRef.forward, out hit))
    132.  
    133. {
    134.  
    135. //Beacuse raycast is true only when it hits anything, we don't need to check if hit is null
    136.  
    137. //We set pointerOver to whatever object the raycast hit.
    138.  
    139. pointerOver = hit.collider.gameObject;
    140.  
    141. //We set the line visual to stop and the point the raycast hit the object.
    142.  
    143. line.SetPosition(1, hit.point);
    144.  
    145.  
    146.  
    147. //Here we check if the object we hit has the PropBase component, or a child class of its.
    148.  
    149. if (pointerOver.GetComponent<PropBase>())
    150.  
    151. {
    152.  
    153. //We set the object to be highlighted
    154.  
    155. selectVisual.NewObject(pointerOver);
    156.  
    157. }
    158.  
    159. else
    160.  
    161. {
    162.  
    163. selectVisual.ClearObject();
    164.  
    165. }
    166.  
    167. }
    168.  
    169. else
    170.  
    171. {
    172.  
    173. //If the raycast hits nothing we set the line visual to stop a little bit infrount of the controller.
    174.  
    175. line.SetPosition(1, controllerRef.position + controllerRef.forward * 10);
    176.  
    177. selectVisual.ClearObject();
    178.  
    179. }
    180.  
    181.  
    182.  
    183. Debug.DrawRay(controllerRef.position, controllerRef.forward * 10, Color.grey);
    184.  
    185. }
    186.  
    187.  
    188.  
    189. void Intract()
    190.  
    191. {
    192.  
    193. //We set up the input "OculusPrimaryIndexTrigger" in the Input manager
    194.  
    195. if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger))
    196.  
    197. {
    198.  
    199. {
    200.  
    201. selectedObject.Trigger();
    202.  
    203. }
    204.  
    205. //If you have a object that you need to hold down a button to intract with
    206.  
    207. }
    208.  
    209. else if (pointerOver != null)
    210.  
    211. {
    212.  
    213. if (pointerOver.GetComponent<PropBase>())
    214.  
    215. {
    216.  
    217. selectedObject = pointerOver.GetComponent<PropBase>();
    218.  
    219. }
    220.  
    221. else
    222.  
    223. {
    224.  
    225. selectedObject = null;
    226.  
    227. }
    228.  
    229. }
    230.  
    231. else
    232.  
    233. {
    234.  
    235. selectedObject = null;
    236.  
    237. }
    238.  
    239.  
    240.  
    241. }
    242.  
    243.  
    244.  
    245. }
    Now I created a UI interface,and in the canvas I added few buttons, which I want to interact with using this ray, however this ray just goes through them.
    I have added the OVR Input Module script to the Event system and have replaced Graphic Raycaster with OVR ray caster in the canvas and has also changed the render mode to world space in the canvas.
    However, i can still not interact with the UI buttons with this ray.
    Can someone please help me with this issue! Any help will be greatly appreciated.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You're on the right track. Something's obviously missing, but what?

    You say you added OVRInputModule to EventSystem. Did you also remove the previous input module? (You should.)

    On the OVRRaycaster, you have to set the Pointer to the transform that defines the ray. Did you do that?
     
    ROBYER1 likes this.
  3. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    20
    Hey,
    I did remove the previous input module, right now my event system has only event system and OVR Input Module scripts.
    Also, I have set the Pointer to controllerRef, which as in the code shows is the object where the ray is being casted from and is a child to OVRPlayerController.

    //This is a refrance to the object we want the pointer to be cast from.[SerializeField]

    public Transform controllerRef;

    However the ray still goes through the buttons.
     
  4. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    20
    Update: In Canvas>OVR Raycaster, I set Blocking Objects to All and Blocking Mask to everything.
    In Event System> OVR Input Module, I changed Ray Transform from CenterEye Anchor to RightHandAnchor, which contained GoController s a child.
    I also added OVRPhysicsRayCaster to OVRCAMERARIG, and set EventMask to everything and added box colliders to the button so that the ray collides with it.
    It is working now, the ray stops when it collides with the button and also interacts with it when I press the button.
    However, the only problem is that , now I see the gaze ring in the centre of the view every time and it can be moved by moving the ray from the controller along the floor. I tried deleting the gaze ring image from the asset, but then I see a white square instead of it.
    Any suggestions?
     
    Hisa4 and V-J like this.
  5. AccentDave

    AccentDave

    Joined:
    Nov 16, 2015
    Posts:
    43
    I'm still looking for a way to do this easily without having to add colliders to the buttons. I can see in the debugger that my objects are hit. I just can't easily access them. This should be super easy, is it really that rare of an ask?
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It's not "super easy" unless you use somebody else's solution (such as CurvedUI, or something from the Oculus SDK samples). But it's not all that hard if you understand the UI system. You do not need 3D colliders on your UI elements, though you will need one on the canvas.