I am building a WebGL game with mouse input. I use the mouse to control movement and aiming, and I sometimes display a canvas and use the mouse to press buttons.
I lock the cursor (Cursor.lockState = CursorLockMode.Locked) in order to prevent the user from accidentally moving it outside the window. I then create a fake cursor by moving around an image on a canvas. I use Input.GetAxis to get the changes in the mouse position each frame. I use the fake cursor position for movement and aiming, and I can still detect clicks using Input.GetMouseButtonDown().
Problem: Buttons on a Canvas don’t work when the real mouse cursor is locked. Is there a standard way to feed my fake cursor position into Unity’s code so that Canvas elements will work?
Some things I’ve looked at and tried:
-
Set StandaloneInputModule.inputOverride to a custom component derived from BaseInput. Override mousePosition to return my fake cursor position. This only works if the cursor is not locked. StandaloneInputModule stops processing mouse clicks when the cursor is locked (the checks for this are in the base class PointerInputModule).
-
Create my own subclass of PointerInputModule and override Process(). I see some reports of people using this for VR, e.g. talesfromtherift.com/vr-gaze-input/. It might work, but the code looks tricky and I haven’t found good documentation for this. It looks like I’d need to do a lot more than just report a cursor location to get it to work. PointerInputModule itself is pretty complex and checks for CursorLockMode in three different places.
-
Use the new Input System and use a virtual cursor. I’ve seen a bunch of tutorials showing how to set up a gamepad cursor, but nothing about what I’m trying to do.