Change mouse position while invisible?

Hello,

I’m trying to implement a system where the player can switch between 2 controlling mode:

-one where your mouse is invisible, moving the mouse just makes you look around, and you click with a cursor on the middle of the screen

  • one where you cannot look around, the middle cursor is invisible, the mouse is visible, and you use it to click on menus

To do so, I wanted to lock the mouse on the middle of the screen while it is invisible, but as I haven’t found such thing in UE4, I decided to rather plug a function that recenters the mouse to the tick event (with the condition that the mouse is invisible),

However, I founded out that when the mouse is invisible, the “SetMousePosition” node don’t work as intended:

It does recenter the mouse as it’s supposed to, but as soon as I move the mouse again, the cursor gets back to where it where before the recentering function occured

How can I lock my mouse on the center of the screen under those circumstances?

thanks,

SetMousePosition just after you turn your mouse visible, no need for tick or anything like that.

Yeah, I tried that, it kinda works, but when the mouse is invisible, it’s still roaming around the screen, and I need it to stay in the middle (so that I can still click with the cursor in the middle of the screen, and so that I don’t accidentaly click on other stuff)

Why not use a crosshair for normal mode and when you want to see/use your mouse take advantage of playerController-> showMouseCursor bool and setInputModeUIOnly and setInputModeGameOnly ?

The set inputmode to game only hide cursor lock to view port and then set its position to the middle of the screen. Screen position is based on a pixel system aka you need to actually set this based off the screen resolution and change it based on the users screen size. Aka it needs to be scaled or set values for x and set x when game starts or when user changes screen size. but long story short 800x 400y will get you close to the middle set that when you unrightclick also set game back to game and ui this will make your single click events move to location work correctly. if its game only with shown cursor it will consume the first click and require double click events and its not clear why. Thats why. and do the first thing i said when you set inputmode to game only… aka when its in the world its consuming it someplace and when its game only it for some reason doesnt respect the viewport as an object restraint, aka you can spin camera for as long as you have room on mouse pad. It will be on the edge of the screen or run out of the game if its not locked to viewport. when its in UI mode it stops dead when it hits the edge but its hidden so it doesnt actually show it hitting the edge and stopping. This is true for your click events it basically has a higher degree of focus for game events regarding widgets and click events. But when its game only it overrides those for as far as i can tell no reason. it basically just nullifies the first left click almost 100% of the time across the project at completely random times.

You can try to get the mouse cursors location but it gets world location not screen location and it wont actually set it even close to the middle if say you move