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;
myInputmode.SetLockMouseToViewport(true);
ThisPlayerController->SetInputMode(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();
}
SetActorLocation(newLocation);
}