Hello,
I have an ALPlayerController class that inherits from APlayerController, in which I am doing the usual input actions such as moving the character forward, strafing and turning (by calling AddControllerYawInput).
Thing is, I also wanted to set it so that if both the two mouse buttons were pressed at the same time, it would be equivalent to moving forward. The way I did it was setting two booleans and checking them in the Tick function, and it works fine, but…
When I press the two mouth buttons + S, the character doesn’t move (works fine), but when i also press the A or D key to turn, it suddenly gets moving instead of just turning while staying still. In fact, the resulting movement is pretty much equivalent to moving forward + turning, but with a different orientation. Supposedly the forward and backward movement gets zeroed as they are opposed, why is there any movement when i call the AddControllerYawInput() ??
Note: The Acceleration displayed in the Tick() function is not zero when this happens.
The code for the controller:
#include "Lynaeorium.h"
#include "LPlayerController.h"
#include <Lynaeorium/Components/NewPlayerMovement.h>
ALPlayerController::ALPlayerController()
{
bCameraFreeLookOn, bCameraRotationOn = false;
}
void ALPlayerController::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
if (bCameraRotationOn && bCameraFreeLookOn)
GetPawn()->AddMovementInput(GetPawn()->GetActorForwardVector(), 1.f);
if (GEngine) GEngine->AddOnScreenDebugMessage(0, 2.f, FColor::Magenta, GetCharacter()->GetCharacterMovement()->GetCurrentAcceleration().ToString());
}
void ALPlayerController::ActivateCameraFreeLook()
{
bCameraFreeLookOn = true;
}
void ALPlayerController::DeactivateCameraFreeLook()
{
bCameraFreeLookOn = false;
}
void ALPlayerController::ActivateCameraRotation()
{
bCameraRotationOn = true;
}
void ALPlayerController::DeactivateCameraRotation()
{
bCameraRotationOn = false;
}
void ALPlayerController::Jump()
{
if (PlayerMovementIsValid()) PlayerMovement->SetJump();
}
void ALPlayerController::SetForwardMovement(float AxisValue)
{
GetPawn()->AddMovementInput(GetPawn()->GetActorForwardVector(), AxisValue);
}
void ALPlayerController::SetStrafeMovement(float AxisValue)
{
GetPawn()->AddMovementInput(GetPawn()->GetActorRightVector(), AxisValue);
}
void ALPlayerController::SetTurnMovement(float AxisValue)
{
GetPawn()->AddControllerYawInput(FMath::Clamp(AxisValue, -1.f, 1.f));
}
bool ALPlayerController::PlayerMovementIsValid()
{
if (PlayerMovement) return true;
PlayerMovement = Cast<UNewPlayerMovement>(GetCharacter()->GetCharacterMovement());
return PlayerMovement ? true : false;
}
void ALPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAction("CameraFreeLook", IE_Pressed, this, &ALPlayerController::ActivateCameraFreeLook);
InputComponent->BindAction("CameraFreeLook", IE_Released, this, &ALPlayerController::DeactivateCameraFreeLook);
InputComponent->BindAction("CameraRotation", IE_Pressed, this, &ALPlayerController::ActivateCameraRotation);
InputComponent->BindAction("CameraRotation", IE_Released, this, &ALPlayerController::DeactivateCameraRotation);
InputComponent->BindAction("Jump", IE_Pressed, this, &ALPlayerController::Jump);
InputComponent->BindAxis("Forward", this, &ALPlayerController::SetForwardMovement);
InputComponent->BindAxis("Strafe", this, &ALPlayerController::SetStrafeMovement);
InputComponent->BindAxis("Turn", this, &ALPlayerController::SetTurnMovement);
}