Camera Attachment at runtime

Hey Guys,
im kind of stuck right now, and i need someone who can explain a few things.

I want to create a top-down camera system where you’ll be able to detach the camera from the main character and use it like a ‘free-look’ camera.

  • The free look part is done, it’s basically just my main camera class which has all the movement functions, zoom etc. Component-wise it’s a camera on a spring arm attached to a static mesh.
  • The playercontroller will spawn the main character and the camera on beginplay and manage the attachment of the camera

Now the difficult part:
I want to switch the attachment of the camera at runtime (e.g. when i press space) from the camera static mesh to the main character.

  • Is it possible to attach the springarm and the camera on this spring arm to another object at runtime?
  • Do i need to switch the possession from the camera to the character? If yes, why? (tha camera component has movement logic as well as the character)
  • Below is the code for the attachment, keep in mind it’s very unfinished. When i test this in the editor, the camera on the gamecharacter is inside of it, however in the details panel the character does not even have a cameracomponent.

[SPOILER]


void AGamePlayerController::v_UnDockCamera()
{
    if( b_CameraDocked )
    {
        // UnDock
        UnPossess();
        Possess( Cast<ACameraPawn>( m_Spawned_Camera ) );

        m_Spawned_Camera->pc_GetSpringArmComponent()->AttachToComponent( m_Spawned_Camera->GetParentComponent(), FAttachmentTransformRules::SnapToTargetIncludingScale );

        b_CameraDocked = false;
    }

    else
    {
        // Dock
        UnPossess();
        Possess( Cast<ACharacter>( m_Spawned_GameCharacter ) );
        b_CameraDocked = true;

        m_Spawned_Camera->pc_GetSpringArmComponent()->AttachToComponent( m_Spawned_GameCharacter->GetCapsuleComponent(), FAttachmentTransformRules::SnapToTargetIncludingScale );

    }
}

[/SPOILER]

Don’t switch camera attachment, use pawns with the right behaviour and keep both around changing possession as needed.