Mouse temporarily leaves window at right and bottom screen edges
I am developing a small FPS and am having some issues with mouse input at the screen edges. Specifically, when I turn right or down long enough, the (invisible) mouse cursor will hit either the right or botton screen edge. When this happens it seems to be leaving the window, but only by a few pixels. I can actually move it around and when it fullscreen it doesn't tab me out, but mouse clicks of any kind aren't registered anymore. When in windowed mode, the cursor occasionally becomes visible, and I can resize the window by clicking and dragging the window edge. This happens both in my game as well as with the unmodified C++ FPS example that comes with the engine. This also appears to happen when I use "Launch game" from the editor.
This video demonstrates the problem: https://www.youtube.com/watch?v=7sVdgFioG5I&feature=youtu.be
asked Mar 24 '18 at 12:47 PM in Everything Else
One workaround is to set the position of the mouse every frame while the viewport is captured so as to prevent the mouse from getting near the edge of the screen. You could constantly set the mouse to be in the center of the screen (as below), or alternatively remember the cursor's last position and keep it there while the mouse is captured by the viewport.
Adding the below code to your PlayerController's Tick function should do the trick:
Edit: When trying to implement the second method I described above (saving the mouse position and setting the saved position while the cursor is captured), I noticed that strangely, SetMouse has different behavior for PIE windows and standalone windows.
For standalone windows, everything works correctly, but when setting the saved position in editor windows, the result would be drastically offset.
Looking through the answerhub, it didn't seem like there was a solution, but after a lot of debugging I found that multiplying the saved mouse coordinates by the viewport resolution's Y/X ratio would give extremely good results (maybe not 100% perfect? or maybe it's just rounding?)
If anyone else has the same issue, try out something like the below code:
Edit 2: I noticed that with the above code active, ejecting from the player character and moving environment data around with the mouse in PIE mode no longer functions because the mouse cursor will get stuck in one position when clicking on the viewport. I couldn't find a way of checking if the editor is in "Possess" or "Eject" mode, so instead I temporarily resolved this by disabling the functionality when in PIE mode. Note that the original bug doesn't seem to occur in PIE mode anyway, so this is a decent enough workaround, I guess.
Follow this question
Once you sign in you will be able to subscribe for any updates here