Download

Mouse won't lock to standalone viewport

Hi,

I’ve created a system that moves my camera depending on the mouse position. It is an RTS game. For some weird reason, whenever the mouse is outside of the viewport, the camera starts to move to one of the set directions (one of the if-s statements is activated).

I tried to launch my game in standalone viewport, and camera started to move with no stopping, so I tried to lock my mouse to the viewport and set it to the middle of it (so the camera shouldn’t move once game starts).

In my GameMode.cpp I added a Begin Play() function, so I can set the location and lock:


 void AVikingKingGameMode::BeginPlay()
 {
     Super::BeginPlay();
     GEngine->GameViewport->Viewport->LockMouseToViewport(true);
     GEngine->GameViewport->Viewport->SetMouse(GEngine->GameViewport->Viewport->GetSizeXY().X / 2, GEngine->GameViewport->Viewport->GetSizeXY().Y / 2);
 }

yet when the game starts, the mouse location is not even taken into consideration, though the camera doesn’t move anymore.

Any suggestions?

Maybe i’m wrong but i think you have to do it with the Playercontroller.

Quick Search: FViewport::LockMouseToViewport isn't working? - UE4 AnswerHub

Yeah, I saw that post as well. Didn’t make a connection though.

I tried doing it through the player controller with no luck:

H file (AVikingKingPlayerController.H):


virtual void BeginPlay() override;

CPP file:


void AVikingKingPlayerController::BeginPlay()
{

	Super::BeginPlay();
	GEngine->GameViewport->Viewport->LockMouseToViewport(true);
	GEngine->GameViewport->Viewport->SetMouse(GEngine->GameViewport->Viewport->GetSizeXY().X / 2, GEngine->GameViewport->Viewport->GetSizeXY().Y / 2);
}

(the function is public in the H file)

Forget what i wrote

Edit:

Actually this seems to be a bug, try to go fullscreen (alt + enter) then leave it again. After that the mouse should be captured

https://answers.unrealengine.com/questions/36446/how-can-i-capture-the-mouse-in-window-mode.html

Still no… and the mouse doesn’t get actually locked…

I tested it myself and (without coding anything) my mouse gets captured when switching to fullscreen and back

Play as standalone->Click on the viewport so it has focus → alt + enter-> alt+enter…
mouse doesn’t lock.
What am I missing?

Did you find a solution for this? I am having the same problems (UE 4.24.2)