I’m trying to make the basic movement for my player character and I’ve only been implementing code for the player controller. There’s literally nothing else in the project except a base Character class that I’m controlling.
The idea was that my character would move forward based on the camera’s forward vector (think Resident Evil4-5-6 or pretty much any third person shooter with over the shoulder camera). This means that the character’s mesh would need to rotate towards the camera’s forward vector BUT only when I’m actually moving the character. If I’m standing still I should be able to rotate the camera and look a the character however I want.
void AISAPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAxis(TEXT("MoveForward"), this, &AISAPlayerController::MoveForward);
InputComponent->BindAxis(TEXT("StrafeRight"), this, &AISAPlayerController::StrafeRight);
InputComponent->BindAxis(TEXT("LookUp"), this, &AISAPlayerController::LookUp);
InputComponent->BindAxis(TEXT("LookRight"), this, &AISAPlayerController::LookRight);
}
void AISAPlayerController::MoveForward(float axisValue)
{
FVector cameraForwardVector = PlayerCameraManager->GetActorForwardVector();
aPlayerCharacter->FaceRotation(cameraForwardVector.ToOrientationRotator());
aPlayerCharacter->AddMovementInput(cameraForwardVector,axisValue);
}
void AISAPlayerController::StrafeRight(float axisValue)
{
FVector cameraForwardVector = PlayerCameraManager->GetActorForwardVector();
FVector characterRightVector = aPlayerCharacter->GetActorRightVector();
aPlayerCharacter->FaceRotation(cameraForwardVector.ToOrientationRotator());
aPlayerCharacter->AddMovementInput(characterRightVector,axisValue);
}
void AISAPlayerController::LookUp(float axisValue)
{
aPlayerCharacter->AddControllerPitchInput(fVerticalSensitivity*axisValue*GetWorld()->GetDeltaSeconds());
if(aPlayerCharacter->GetVelocity().Length() > 2.f)
{
FVector cameraForwardVector = PlayerCameraManager->GetActorForwardVector();
aPlayerCharacter->FaceRotation(cameraForwardVector.ToOrientationRotator());
}
}
void AISAPlayerController::LookRight(float axisValue)
{
aPlayerCharacter->AddControllerYawInput(fHorizontalSensitivity*axisValue*GetWorld()->GetDeltaSeconds());
if(aPlayerCharacter->GetVelocity().Length() > 2.f)
{
FVector cameraForwardVector = PlayerCameraManager->GetActorForwardVector();
aPlayerCharacter->FaceRotation(cameraForwardVector.ToOrientationRotator());
}
}
I can move around but I wasn’t getting the behavior I wanted, namely, the character was constantly rotating along with the camera rather than rotating only when moving.
So I started drawing debug arrows along the forward vector:
void AISAPlayerController::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
debugCurrentRotation = aPlayerCharacter->GetActorRotation();
debugCurrentCameraRotation = PlayerCameraManager->GetCameraRotation();
// Camera forward vector
UKismetSystemLibrary::DrawDebugArrow(GetWorld(),PlayerCameraManager->GetCameraLocation(),PlayerCameraManager->GetActorForwardVector()+180.f,10.f,FLinearColor::Red,1.f,6.f);
// Character forward vector
UKismetSystemLibrary::DrawDebugArrow(GetWorld(),aPlayerCharacter->GetActorLocation(),aPlayerCharacter->GetActorForwardVector()+180.f,10.f,FLinearColor::Yellow,1.f,6.f);
}
And it’s definitely not working as expected, since I have this result:
Player start
Strafe left while still looking forward
I honestly don’t know what I’m doing wrong here, I’ve been using ForwardVectors during online courses several times but I’ve never seen such behavior. Keep in mind, this code is the ONLY thing currently implemented in the project besides having set the default pawn to my character in the game mode settings, but this is also the first time I use the PlayerCameraManager, and it’s the default one spawned with APlayerController.
The same area is pointed regardless of where I place my PlayerStart, which is why I titled the post with “world origin”