I want the following behavior:
- If i don’t click any mouse button i want to see the mouse cursor and can’t move the camera
- if i click the right mouse button i
want to rotate the camera like the
default third person template does.
To achieve this behavior i extended the c++ third person template with following functions:
void AHerobattleCharacter::LockCamera()
{
if (MyController)
{
MyController->SetIgnoreLookInput(false);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("LockCamera"));
MyController->bEnableClickEvents = false;
MyController->bEnableMouseOverEvents = false;
bCameraIsLocked = true;
}
}
// called when the right mouse get released
void AHerobattleCharacter::ReleaseCamera()
{
if (MyController)
{
MyController->SetIgnoreLookInput(true);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("ReleaseCamera"));
MyController->bEnableClickEvents = true;
MyController->bEnableMouseOverEvents = true;
bCameraIsLocked = false;
}
}
I added the following lines to the SetupPlayerInputComponent Function:
InputComponent->BindAction("RightMouse", IE_Pressed, this, &AHerobattleCharacter::LockCamera);
InputComponent->BindAction("RightMouse", IE_Released, this, &AHerobattleCharacter::ReleaseCamera);
I also added the BeginPlay function:
void AHerobattleCharacter::BeginPlay()
{
Super::BeginPlay();
if (Controller)
{
initializeMouse();
}
}
void AHerobattleCharacter::initializeMouse()
{
MyController = GetWorld()->GetFirstPlayerController();
//Input Mode for hiding courser
FInputModeGameAndUI InputMode;
InputMode.SetHideCursorDuringCapture(true);
InputMode.SetLockMouseToViewport(true);
MyController->SetInputMode(InputMode);
MyController->SetIgnoreLookInput(true);
MyController->bShowMouseCursor = true;
MyController->bEnableClickEvents = true;
MyController->bEnableMouseOverEvents = true;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("MouseInit"));
}
When I run the game as a single player game, the mouse works as intended. But when I run the game with multiple players, the mouse cursor is hidden and I can rotate the camera as if I would have pressed the right mouse button. If I bind the initializeMouse function to a key and press this key ingame the mouse cursor appears and works like intended.
According to this
https://answers.unrealengine.com/questions/183488/player-controller-get-controlled-pawn-client-delay.html the player doesn’t have a PlayerController from start and its better to make changes when the PostLogin event is called. So I removed the initializeMouse function from my BeginPlay function and added the following code to my gamemode:
void AHerobattleGameMode::PostLogin(APlayerController * NewPlayer)
{
Super::PostLogin(NewPlayer);
AHerobattleCharacter* currentPlayer = (AHerobattleCharacter*)(NewPlayer)->GetControlledPawn();
if (currentPlayer)
{
currentPlayer->initializeMouse();
}
}
But that didn’t resolve my issue.
Anyone has an idea, why my code doesn’t work?