Hi,
I am using Unreal Engine 5 to build a short demo, but I am having some issues with Pawn possession.
When the game starts, a default pawn of my choice is possessed. This has the following View from the camera:
I then spawn an object into the scene and posses that character using the following code:
AController* SavedController = GetController();
SavedController->UnPossess();
AActor* SpawnActorRef = GetWorld()->SpawnActor<AActor>(ActorToSpawn, FVector(0.0f, 0.0f, 15.0f), FRotator(0.0f, 0.0f, 0.0f), SpawnParams);
SavedController->Possess(Cast<APawn>(SpawnActorRef));
This works fine and gives me the following view from the camera (this is the camera view that’s been configured on the new pawn) With this, I can control the object shown on screen.
However, the problem now comes when I try to repossess the initial pawn that’s loaded into the scene, this is a blueprint pawn which I find using the following code:
TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AGameDefaults::StaticClass(), FoundActors);
UE_LOG(LogTemp, Warning, TEXT("%s"), *FoundActors[0]->GetName()) // This returns the correct pawn
if (FoundActors.Num() != 0)
{
AController* SavedController = GetController();
SavedController->UnPossess();
SavedController->Possess(FoundActors[0]->GetWorld()->GetFirstPlayerController()->GetPawn());
}
But when I repossess the initial pawn, I get the following view:
Can anyone explain why this happens? Upon checking the UE_LOG, the correct pawn is found, but the camera view is not maintained. One thing I noticed is that if I eject from the game, the default pawn camera cannot be seen in the scene, so I’m not sure if that’s related to the issue.
Any insight on this would be appreciated.