Mouse input going out of bounds

I am trying to move my camera when it reaches a border of my game window, the code runs fine when it reaches exactly the side of the window, but when I keep moving my mouse against the border, the input of the mouse seems to go out of bounds, on both x and y axis.
So it starts moving to the left top corner instead of only to the top, the same happens when I try to move to the left side as well, it works fine when I move to the exact border but goes out of bounds when I keep moving the mouse against the border:
the first print screen my mouse it at the exact top, the second print screen is when I try to push my mouse over the border.

So is there a way to avoid this behavior? this is my current code

void ACameraPawn::MoveCameraWithMouse(float DeltaTime)
	FVector2D viewportSize;
	if (GEngine)
		viewportSize = FVector2D(GEngine->GameViewport->Viewport->GetSizeXY());
	float x, y;
	ThisPlayerController->GetMousePosition(x, y);
	FString debugString = FString::FromInt(x) + " " + FString::FromInt(y);
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, debugString);
	FInputModeGameAndUI myInputmode;

	FVector newLocation = GetActorLocation();

	if (x < MouseMoveMargin )
		newLocation -= CameraMoveSpeed * DeltaTime * GetActorRightVector();
	else if (x > viewportSize.X - MouseMoveMargin)
		newLocation += CameraMoveSpeed * DeltaTime* GetActorRightVector();

	if (y < MouseMoveMargin)
		newLocation += CameraMoveSpeed * DeltaTime * GetActorForwardVector();
	else if (y > viewportSize.Y - MouseMoveMargin)
		newLocation -= CameraMoveSpeed * DeltaTime * GetActorForwardVector();


Try checking out the documentation on FViewport at FViewport | Unreal Engine Documentation, you may need to lock your mouse, or perform some other fancy check on your viewport.

my mouse is locked to the viewport, but I cant seem to hold it from sometimes jumping out of bounds. It is also very strange that it only goes wrong in the left and upper side of the viewport, while moving my cursor to the right or bottom corner it does not go out of bounds.

Hello KoenVandenSteen,

It look like GetMousePosition(…) method does not assign out values when cursor is out of screen bounds, so, you have this values because your x and y coordinates is not initialized.
You can try to initialize your x and y coordinates.

Hope this helps

yes that’s probably it, but the problem is still that if I go out of bounds on only the top side, both of my parameters go out of bounds, so I cant know which way the camera should be moving, but I will have to find some work around then…

it is also very strange that when I try this on the bottom side of the view port, or the right side the lock mouse in view port function does work correctly.

I fixed it with initializing the x and y on -1 and then I can check whether the new input is a valid one or not, if valid I use the new input values, else I use the previous ones. Works pretty smooth now:

if (x >= 0)
	PreviousMousePos.X = x;
if (y >= 0)
	PreviousMousePos.Y = y;