I’ve set up my class ADynamicCamera, which contains UCameraComponent & USpringArmComponent to change the camera location dynamically in my 2D top down shooter.
I tried to change the viewtarget to the ADynamicCamera* member variable in the controlled pawn but I’m unable to have it change, as it reverts back to the controlled pawn as view target, with some default camera view from the side (as I haven’t got a camera on the pawn).
The ADynamicCamera actor gets spawned and attached to the pawn, here’s the code that then gets called when the pawn become possessed. ( The actor exists, so that’'s not the problem)
void ACrewMember::PossessedBy(AController* controller)
{
Super::PossessedBy(controller);
AMSSPPlayerController* ctrl = Cast<AMSSPPlayerController>(controller);
if (ctrl)
ctrl->SetViewTarget(DynamicCamera);
}
Fixed the issue. Apperantly a pawn cannot change viewtarget while becoming possessed.
Added the same code but placed it in the tick function of the pawn instead like so
AMSSPPlayerController* ctrl = Cast<AMSSPPlayerController>(Controller);
if (ctrl != nullptr && ctrl->GetViewTarget() != DynamicCamera)
{
ctrl->SetViewTarget(DynamicCamera);
}
and that solved it for me!
I have found an alternative solution when I had to change the view to a stationary world camera at the start of play.
So, I found this method: BeginPlayingState()
It appears to be called just after possession has taken place and is empty and thus safe to override for your own use!
For demonstration purposes, I’ve provided my [not particularly efficient] code below:
void APlayerController_SpaceApe::BeginPlayingState() {
FString CameraName = "MainGameCameraActor";
if (GetWorld()) {
for (TActorIterator<AActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
// Access the subclass instance with the * or -> operators.
if (ActorItr->GetName().Contains(CameraName)) {
FViewTargetTransitionParams Params;
SetViewTarget(*ActorItr, Params);
UE_LOG(LogTemp, Warning, TEXT("BeginPlayingState - SetViewTarget"));
}
}
}
}