Hello everyone,
Today I have been trying to move my spring arm and camera from the pawn into the player controller. The reason for this is that I want to use the pawn for ai controllers as well and then a camera doesn’t make sense in the pawn anymore.
I converged on the following solution:
UCLASS()
class GAME_API AMyPlayerController : public APlayerController
{
GENERATED_BODY()
public:
virtual void Possess(APawn* InPawn) override;
virtual void UnPossess() override;
private:
USpringArmComponent* SpringArmComponent;
UCameraComponent* CameraComponent;
};
The implementation of Possess is as follows:
void AMyPlayerController::Possess(APawn* InPawn)
{
Super::Possess(InPawn);
if (InPawn)
{
SpringArmComponent = NewObject<USpringArmComponent>(InPawn, TEXT("SpringArmComponent"));
SpringArmComponent->TargetArmLength = 1000.0f;
SpringArmComponent->bEnableCameraLag = true;
SpringArmComponent->bEnableCameraRotationLag = true;
SpringArmComponent->CameraLagSpeed = 3.0f;
SpringArmComponent->CameraRotationLagSpeed = 3.0f;
SpringArmComponent->RegisterComponent();
CameraComponent = NewObject<UCameraComponent>(InPawn, TEXT("CameraComponent"));
CameraComponent->RegisterComponent();
CameraComponent->AttachTo(SpringArmComponent, USpringArmComponent::SocketName);
SpringArmComponent->AttachTo(InPawn->GetRootComponent());
}
}
And finally the implementation of UnPossess:
void AMyPlayerController::UnPossess()
{
Super::UnPossess();
CameraComponent->DestroyComponent();
SpringArmComponent->DestroyComponent();
}
This works, but my question is, is this the right way to go about for having the controller in charge of which pawn the camera is attached to? I have seen people overriding the PlayerCameraManager but it seems unnecessary to me, but I may be missing something. I would love to hear your opinions and ideas.
Thanks!