GamePad Stick/Buttons as Mouse Move/Click UI

Hello,

I try to do a system that switch between the Mouse and the Gamepad for UI control.
I need : Left stick of the Gamepad control the Cursor and Gamepad button control the left Mouse click

I Use :
The new InputSystemUIInputModule.
For the moment I move the cursor with this function : Mouse.current.WarpCursorPosition();
I made my asset control with UI map actions. And I add gamepad binding for Click, Point and Move.

But all events (over, pointerdown, etc) doesn't work with game pad.

Someone have an idea to achieve this system with new InputSystem?

Thanks a lot for your help

Built-in support for this is on the list for after 1.0.

For now, I’d recommend adding a virtual mouse and driving that. This will generally work better than trying to hijack the system mouse with WarpCursorPosition.

var mouse = InputSystem.AddDevice<Mouse>();

// Somewhere in update.
var mouseDelta = gamepadInputAction.ReadValue<Vector2>() * m_MouseSpeed;
var currentPosition = mouse.position.ReadValue();
InputSystem.QueueStateEvent(mouse,
    new MouseState
    {
        position = currentPosition + delta,
        delta = delta,
        // Set other stuff like button states...
    });
1 Like

Just for reference, 1.0.0-preview.5 will come with an example for how to set up a gamepad mouse cursor which also contains a reusable component that does most of the work.

1 Like

I was googling around for a way to control my cursor with my controller, and found this gem of a post buried in the results. Saved me a huge headache!

1 Like

This discussion was an absolute life saver! Thank you so much!

I was able to understand whats happening here for the most part and I really like this is available. I'm trying to do something kind of similar which is to add input from an Oculus controller to a virtual gamepad. I am wondering what the new MouseState is, is that an API in Unity? Is there one similar to gamepads?

[quote=“Rene-Damm, post:3, topic: 762533, username:Rene-Damm”]
Just for reference, 1.0.0-preview.5 will come with an example for how to set up a gamepad mouse cursor which also contains a reusable component that does most of the work.
[/quote] Installed that example, how do I switch the mouse on and off, so that I can run and fire with the gamepad for example. Any object i should disable?
The example automatically switches to the action set of UI, but I can’t seem to be able switch that off.
Thank you

1 Like

I had the same problem- just do some checks and if you want to disable the VirtualMouseInput script disable it at the end of OnEnable- I wanted it to disable if there are no gamepads so I put this at the end of OnEnable on that script

 if (Gamepad.all.Count == 0)
            {
                  this.enabled = false;
            }
            else
            {
                Debug.Log(Gamepad.all.Count + " gamepad's detected");
            }
2 Likes

I don't know if it would be helpful for a future me BUT

[Version 1.4.4 - November 03, 2022]

The example scene of the gamepad cursors is broken by mistake.

To fix that you need to set the blocking mask of the graphic raycaster into the canvas to 2D ( In the example import blocking mask is NONE causing the example to not work )

Then, a tip for a future me. Use the virtual cursor only for the gamepad cursor, please i see too many people make this error and use them together with the actual game mouse. They don't work well together.

But you can use the controls scheme to actually detect the correct scheme to activate the gamepad virtual cursor. In this way, you can quickly swap virtual cursor component (Only for Gamepad schemes) and the real cursor (Cursor API/Custom Pointer Implementation) (Only for keyboard/mouse schemes)