[Tutorial] How to control UMG widget that uses mouse cursor with both mouse and gamepad

Working on my current game project I’ve came to the point where I needed to make UMG user interface which can be controlled both by mouse and gamepad. There were certain obstacles while I was trying to make this system work. Now I’m ready to post this little tutorial with the example of working result. Please note that the tutorial is based only on Blueprints as my project is 100% Blueprints-based and I have no experience of working with C++ directly.

This is how my game moves mouse cursor with gamepad stick. The idea is to use GetGamepadLeftThumbstickX-Axis and GetGamepadLeftThumbstickY-Axis, round them, multiply the results by desired sensitivity, and set coordinates for SetMousePosition node with restrictions tied to viewport size. The entire script needs to be put into player controller of your game project. Screenshot below shows script that works perfectly in my game (the PressAnyKey part in the script is not tied to the cursor movement, but may also be handy, so I decided to capture the entire PlayerController script from my game):

DetectStickMovement macro is needed to determine if the stick is actively moving to avoid interference with mouse movement when the stick is inert:

I also use complicated system that allows game’s UI to be controlled by hotkeys and gamepad keys. But this will require much more time to describe. I’ll do this when I’ll have some free time.

Hope this tutorial will help those, who also want to use gamepad in their games this way :slight_smile:

4 Likes

Thank you! Thats work very well in UE5.02 since the Virtual Cursor plugin isn’t updated yet.

The only think I noticed is that if I start pressing the joystick in the X axis it not register changes in the Y axis until I release the joystick back to the neutral positicon. And it also happens in reverse: if I start moving it in the Y axis it no register any X axis movement until I release the joystick. Is a minor issue for me because I use it for menus, but I assume it will be more severe for others.

Anyway, thanks ^^

1 Like

You are welcome!

I’ll try to reproduce issue you mentioned and think what can be done with it when I’ll have time. Thanks for the feedback! :slight_smile:

1 Like

@RedSpotSylphina
Hi, do you have an example of pressing button A from a gamepad

copiado tal cual y no funciono

@solokapa, you probably made a mistake in your script. Or, if you are trying to use PS4 gamepad on PC, then you need DS4Tool in order to make your PC detect attached gamepad.

@GX234
Hi! :slight_smile: As far as I remember, “A” button on XBox gamepad is marked as Gamepad Face Button Down in the engine. So, you need to choose this button in the node that detects pressed button, and connect the subsequent branch with the next node with True pin instead of False pin.

Beautiful work. Thank you for sharing.

1 Like