how to change camera to a component camera at runtime c++

I have a component camera that is attached to my player controller so that when I possess the pawn it will view the possessed pawn, but the pawn and the player controller are only spawned at runtime. So, I’m having trouble figuring out how to set the active camera to that component camera because the editor defaults to some generated camera it has because the pawn and the controller are being generated after the fact. Thanks for any insight you might be able to provide on this.

You’ll need to grab the active PlayerController and call SetViewTarget. Pass it your active pawn.

You can do something like this within your Pawn code (assuming your pawn has an active UCameraComponent called MyCamera):

APlayerController* ActivePC = UGameplayStatics::GetPlayerController(this, PlayerIndex);
if (ActivePC && MyCamera) {
    ActivePC->SetViewTarget(this);

}

You can also pass SetViewTarget an FViewTargetTransitionParams argument that defines the transition behavior from the current camera to the new camera. By default it’s a hard cut.

If you only want to use one camera, or otherwise want the Pawn to automatically take over the camera as soon as it’s ready, you can just place that code within your Pawn’s BeginPlay event.

If you want to have more control (or conditionally change between different AActor’s UCameraComponents) you should place that in a method that’s called when you want to switch to that (Actor/Pawn)'s camera.