Hello,
I am currently trying to separate my camera component from my pawn. I believe the pawn should represent the entity the player or the ai is controlling and nothing more. A camera does not make sense when the ai is controlling the pawn. Therefore I think a camera does not belong in the pawn.
I have created a separate camera actor that is instantiated by the player controller. I then set the view target for the player controller to the camera actor. Any input related to the camera view is then forwarded to the camera actor by the player controller, and not to the pawn. Whenever I receive input which should act on the pawn, I have the player controller direct it to the pawn.
This is the header file for the camera actor:
UCLASS()
class EXPANSE_API ATopDownCameraActor : public AActor
{
GENERATED_BODY()
public:
ATopDownCameraActor();
void AddHorizontalMovementInput(float Value);
void AddVerticalMovementInput(float Value);
virtual void Tick(float DeltaTime) override;
private:
float HorizontalMovementInput;
float VerticalMovementInput;
UPROPERTY(VisibleAnywhere)
USpringArmComponent* SpringArmComponent;
UPROPERTY(VisibleAnywhere)
UCameraComponent* CameraComponent;
};
The implementation of the camera actor:
ATopDownCameraActor::ATopDownCameraActor()
{
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->SetRelativeRotation(FRotator(-75.0f, 0.0f, 0.0f));
HorizontalMovementInput = 0.0f;
PrimaryActorTick.bCanEverTick = true;
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
SpringArmComponent->TargetArmLength = 300.0f;
SpringArmComponent->TargetOffset = FVector(0.0f, 0.0f, 1200.0f);
VerticalMovementInput = 0.0f;
CameraComponent->AttachTo(SpringArmComponent);
RootComponent = SpringArmComponent;
}
void ATopDownCameraActor::AddHorizontalMovementInput(float Value)
{
HorizontalMovementInput = Value;
}
void ATopDownCameraActor::AddVerticalMovementInput(float Value)
{
VerticalMovementInput = Value;
}
void ATopDownCameraActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector MovementInputVector = FVector(VerticalMovementInput, HorizontalMovementInput, 0.0f);
FVector NewLocation = GetActorLocation() + MovementInputVector * DeltaTime;
SetActorLocation(NewLocation);
}
And the relevant section of the player controller:
void AArmyPlayerController::BeginPlay()
{
TopDownCameraActor= GetWorld()->SpawnActor<ATopDownCameraActor>(ATopDownCameraActor::StaticClass());
SetViewTarget(TopDownCameraActor);
}
Is this the proper way to go about? How have others separated the camera from the pawn? I am looking for opinions since I am quite new to the engine. Thanks!