Limiting mouse movement. How to retain fine controll over character rotation?

I have made bounding box, to limit curos movement to certain area of screen, like this:

ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(GetOwningPlayerController()->Player);

if (LocalPlayer)
{
	FViewport* Viewport = LocalPlayer->ViewportClient->Viewport;

	//FVector2D ViewportSize = GEngine->GameViewport->Viewport->GetSizeXY();
	FVector2D MouseMaxTopLeft;
	FVector2D MouseMaxBottomRight;
	FVector2D MousePos;
	GetOwningPlayerController()->GetMousePosition(MousePos.X, MousePos.Y);
	MouseMaxTopLeft.X = (Canvas->ClipX * 0.5f - 40.0f)-100;
	MouseMaxTopLeft.Y = (Canvas->ClipY * 0.5f + 56.0f)-100;
	MouseMaxBottomRight.X = (Canvas->ClipX * 0.5f - 40.0f) + 100;
	MouseMaxBottomRight.Y = (Canvas->ClipY * 0.5f + 56.0f) + 100;

	DrawRect(FLinearColor::Red, (Canvas->ClipX * 0.5f - 40.0f), (Canvas->ClipY * 0.5f + 56.0f),
		100, 100
		);

	if (MousePos.X > MouseMaxBottomRight.X)
	{
		Viewport->SetMouse(700, MousePos.Y);
		GetOwningPlayerController()->AddYawInput(1);
		GEngine->AddOnScreenDebugMessage(1, 10, FColor::Cyan, FString("Mouse Reached X bound"));
	}
	else if (MousePos.X < MouseMaxTopLeft.X)
	{
		GetOwningPlayerController()->AddYawInput(-1);
		Viewport->SetMouse(500, MousePos.Y);
	}
}

Now. It does work. But when cursor reached border, character stop rotating.

So I added these:

GetOwningPlayerController()->AddYawInput(1);

GetOwningPlayerController()->AddYawInput(-1);

And now character rotates. But at const rate, which is less than ideal.

What I’d like to have is limited cursor (corsshair in that case) movement to certain box extends, and at the same time retain fine grain mouse control over character ? It’s certainly somehow possible since other games, do so, but how do I do it in Unreal ?

You’re doing the clipping manually, which means you know how far the mouse actually moved each frame. It might be enough simply to perform your rotations before you clip the mouse position instead of after.

IIRC with many player controllers, mouse movement changes controller facing by default. You may find yourself wanting to disable that behavior in your player controller and rely entirely on your code above to handle changing player controller facing. That way you can be sure your rotation will always be at a consistent degress per pixel of mouse movement, instead of behaving slightly differently depending on whether the cursor is at the edge of the box or not.