Trying to get relative cursor position for the center of the screen using 'Get Viewport Size'

For context I’m relatively new to Unreal Engine and development so I apologize if this question seems green.

I’m trying to move my Player Character Mesh based on its position from the center of the screen. I am able to accomplish this only if I manually set the offset values. I’m attempting to use the ‘Get Viewport Size’ function to divide the X and Y values by 2 to dynamically provide this value instead.

Expected behavior is that the Player Character Mesh will move relative to my mouse cursor in the center of the screen. Actual behavior is the offset value appears to be ‘off’, as if it’s seeing my viewport as the wrong dimensions. If the cursor ‘center point’ is supposed to be X:1013 and Y: 513, it’s instead X:900 and Y:400.

Is there a better way to get this value or is it better done through C++?

Here’s what we have been using for several projects to center the mouse.

void ABasePlayerController::CenterMouseOnScreen()
{
	if (GetWorld())
	{
		// offset slightly from center
		int centerOffset = 20;
		FViewport* viewport = GetWorld()->GetGameViewport()->Viewport;
		if (viewport)
		{
			FIntPoint size = viewport->GetSizeXY();
			FIntPoint center = size / 2;
			viewport->SetMouse(center.X - centerOffset, center.Y - centerOffset);
		}
	}
}
1 Like

I’ll give this a go, thanks!