1

I'm a beginner of Unity and I've been trying to make an app for the Oculus go. So I implemented the Oculus go Controller working and some buttons on the screen. I can click the buttons and call the functions connected to the buttons. What I wanna do is I want to trigger some action when the controller clicked on not UI buttons.

I've tried these methods but I got false every time even if the button shows highlight color when the pointer is on the button.

Physics.Raycast(..)
EventSystem.current.IsPointerOverGameObject
Ray laserPointer = new Ray(rightHandAnchor.position, rightHandAnchor.forward);
RaycastHit hit;
bool hitOrNot = Physics.Raycast(laserPointer, out hit, maxRayDistance);

and

bool hitOrNot = EventSystem.current.IsPointerOverGameObject;
Yes Barry
  • 9,514
  • 5
  • 50
  • 69

1 Answers1

4

You need to have the OVRInputModule in your scene's EventSystem object (you can delete the "Standard Input Module".

This will allow the laser pointer to interface with the UI system using standard-like events.

The other thing to note is in order for your laser pointer to interact with normal game objects (non-UI), you must add the OVRPhysicsRaycaster to your OVRCameraRig object in your scene.

For the OVRInputModule, you will need set the "Ray Transform" property to either the rightHandAnchor or leftHandAnchor in your LocalAvatar object.

EDIT:

I forgot to mention that your canvas(es) need to have the OVRRaycaster component added as well. The OVRInputModule will basically overload all the normal input events, so your canvas buttons will respond to IPointerEnterHandler events just like they would if you were using a mouse.

using UnityEngine;
using UnityEngine.EventSystems;

public class YourButton : MonoBehavior, IPointerEnterHandler, IPointerClickHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Raycast hit!");
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("Clicked!");
    }
}

With all of this you shouldn't really need to manually do raycasting in your script unless you have special needs for it.

Furthermore, whatever "Event Mask" you select for your OVRPhysicsRaycaster in the camera rig, any normal (non-UI) 3D object that is in that layer will detect the same IPointerEnterHandler and IPointer...Handler events just like a UI button, so you can use the same code for those objects as well, not just the UI objects.

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
  • 1
    I've already added everything you suggested. I can click UI butto. Besides I can get the name of 3D object. but I can't get the name of UI object when the laser is over the object – Discrete Ha Sep 17 '19 at 04:01
  • 1
    Make sure to add the `OVRRaycaster` to your canvas as well. – Yes Barry Sep 17 '19 at 13:35
  • 1
    I'm trying to detect when the laser pointer is simply hovering over the button but somehow I can't. I need to press the trigger button in order to get the methods called. Also, they all get called at the same time. More details here: https://stackoverflow.com/questions/60698832/detecting-hovering-on-button-event-on-oculus-quest-unity3d – Claus Mar 18 '20 at 23:50
  • @DiscreteHa did you find a solution? I think we had the same problem – Claus Mar 19 '20 at 01:53