Getting/Setting Mouse position not working correctly

I boiled down an issue I was having where I was trying to limit the mouse position to inside the aspect ratio of my camera. I was testing this with PIE in the selected viewport. Two things were happening:

  1. When you Shift-F1 for the mouse cursor, the mouse will move continuously to the left. The mouse is fine otherwise when stationary (I have an object following mouse position so I know that it was fine).
  2. If the mouse is not stationary (Not using Shift-F1, just the object following the mouse in game) weird things happen. It seems to be some sort of mouse acceleration issue. If I move my mouse very slowly to the right, the mouse will actually start to veer left. If I move it faster the effect is less obvious. I should mention I have mouse acceleration disabled in Windows.

Simply using this c++ in a Tick will replicate the issue:

FVector2D MousePosition;
APlayerController* Controller = UGameplayStatics::GetPlayerController(GetWorld(), 0);
ULocalPlayer* LocPlayer = Cast<ULocalPlayer>(Controller->Player);
Controller->GetMousePosition(MousePosition.X, MousePosition.Y);
LocPlayer->ViewportClient->Viewport->SetMouse(FMath::RoundToInt(MousePosition.X), FMath::RoundToInt(MousePosition.Y));

I put the RoundToInt for good measure in case of floating point accuracy but it didn’t make a difference either way.

Hey -

Because you are calling this code on tick, it is continually trying to calculate and set the mouse position. When you move the mouse to the right (increase the X), the next tick it will again round the value and if the movement wasn’t enough to round up, it will round down and move the mouse back to the left as you’re seeing.

If I have misunderstood what you are referring to, can you supply a sample project with the issue you’re seeing so that I can investigate the issue directly?

Cheers

The ‘mouse moving to the left’ happens without moving my mouse at all.

I can make a sample, but is there a specific way I should upload it, and should I upload the whole project folder?

If you’re able to create a sample project you can delete the Saved and Intermediate folders and then zip the entire project folder and attach it here. Another solution would be to zip the project and upload it to google drive or Dropbox, then post the download link here or as a private message to me on the forums.

Here we are, it’s a code project, I deleted some of the larger generated VS files otherwise the zip would be 300 MB, hopefully that’s fine. When you use PIE and press shift-F1, the ‘mouse moving left’ thing happens.

Hey -

When I opened the project and used Shift+F1, I did not see the mouse drifting left. I did notice that when the mouse touches any of the four borders it instantly returns to the top left corner of the game window. Can you let me know if you have any extra controllers or other peripherals that may be providing input to the mouse?

Hmm, I don’t have any controllers setup.

Here’s a video of what’s happening just to clarify. It shows the mouse moving left, as well as what you described with the mouse going to the top left corner when they touch the borders.

In addition to the mouse drifting left I noticed the mouse jumping around in the video which is also not happening in the project you sent. I also tried adding the code you provided originally to a new project as well but did not see the mouse behaving as shown in your video.

Yea I’m not sure what’s causing it, I tried changing my mouse settings to default in windows (6/11 mouse speed and mouse acceleration on, I usually have it at 3/11 and mouse acceleration off) but it still happens. Sorry that I couldn’t replicate the conditions. It’s not a blocker for me right now or anything but if it is in the future I could do further testing on other machines to see if it happens anywhere else or if it’s just some one-off on my machine.

Hey -

I’m sorry we were not able to track down the cause of the strange mouse behavior for you though I am glad it is not blocking you from development. If this does continue to be a problem, let me know if you’re able to test/reproduce this on another machine and we can continue to investigate.

Best of luck in your projects,