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;
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.