Can I use IDragHandler to orbit the camera?

I have a script attached to my camera and when I click and drag the mouse I want to rotate the camera around an object. I’ve got this to work using Input.GetMouseButton() and Input.GetAxis(). But I’m curious if I can use IDragHandler and OnDrag instead? I’ve includer UnityEngine.EventSystems, implemented the IDragHandler interface, added a Physics Raycaster component to the camera, and then used the code below to test it, but it does not work.

public void OnDrag(PointerEventData eventData)
{
    Debug.Log("OnDrag called.");
}

I have however tried using OnDrag on a gameobject in the scene that has got a Sphere Collider on it, and then the OnDrag works.

But I just want the camera to listen and react to all clicks or drags even if I don’t actually click on a specific object. The idea is simply to click (with the mouse on a PC or touch with a finger on a tablet) somewhere on the screen and then move the cursor/finger to rotate the camera. The PointerEventData contains a lot off interesting data, but I can’t figure out how to use the EventSystem without relying on physics colliders?

You can make a UI panel stretch over the whole screen and put an Event Trigger on it to look for Drag, then call your camera’s script OnDrag method. But this blocks world objects from getting the physics raycaster from the camera.

To work around that, you can make a second Camera and second Canvas. Set the Depth on the camera smaller then your main (-1) along with culling mask to nothing since you don’t actually want to see anything with it. Set the canvas to screen space camera and drag your second cam in there. Then set up the panel like previously mentioned.

That should allow you to still use the event triggers on world objects, but whenever you Drag without being over a collider, it’ll pick it up.

Also, if you want to only accept Event Triggers on world objects with actual Event Triggers and ignore the rest of the colliders; you can put all the objects you put an event trigger on into a new layer and set the event mask of the Physics Raycaster to that.