I’m making an FPS game with a lot of world space UIs. I want to use a crosshair to click buttons or move sliders.
Normally I use Physics.Raycast() to detect game objects in front of my crosshair, but these UIs don’t have colliders, and Physics.Raycast() can’t detect them.
I tried adding box colliders to them, but the boxes don’t automatically fit (very tiny comparing with UI elements ). Very inconvenient.
So is it a good way to interact with word space UIs with Physics.Raycast()? Are there better ways to do this?
This is a bit old, but since I’ve just recently been messing with this stuff myself, I figured I’d drop a couple helpful links to anyone else who comes across this.
This is doable without using physical colliders, but does require messing with the EventSystem itself.
If you want a controllable crosshair you can move around the screen like a mouse cursor, check this first link –
Adding in a customized input module as described here: https://forum.unity.com/threads/how-to-create-a-mouse-like-cursor-to-be-controlled-by-the-keyboard-or-a-joystick.299656/
My own post in there extends it for slider dragging, limiting the range of the raycaster, and lets you toggle between a free or locked cursor, etc.
If you are only keeping the cursor locked in the middle of the screen and don’t need to move it around, then check out EmmaEwert’s post in https://forum.unity.com/threads/fake-mouse-position-in-4-6-ui-answered.283748/