Set up a camera insider player controller

I lost a few days already and can’t understand it so far, nothing works !
I create the SpringArm and the camera in the contructor, and then I override the Possess function, and try to attach the arm and camera to pawn->GetRootComponent();

Nothing works so far ! Could someone explain it to me ? Or is this even needed, maybe there is a workaround, I was thinking of putting all the controller logic in controller so I could posses any type of character in game, without needing to implements all the camera and spring arm in each on of those.
I took the top down project, copied everything from character contrstructor to controller contructor, left only attachment in posses:

void ATestPlayerController::Possess(APawn* InPawn)
{
	Super::Possess(InPawn);

		if(InPawn)
		{
			CameraBoom->AttachTo(InPawn->GetRootComponent());
			TopDownCameraComponent->AttachTo(CameraBoom, USpringArmComponent::SocketName);
		}
}

Nothing. then I tried something like this:

void ATestPlayerController::Possess(APawn* InPawn)
{
	Super::Possess(InPawn);

		if(InPawn)
		{
			CameraBoom = NewObject<USpringArmComponent>(this, USpringArmComponent::StaticClass());
			CameraBoom->RegisterComponent();
			CameraBoom->AttachTo(InPawn->GetRootComponent());
			CameraBoom->bAbsoluteRotation = true; // Don't want arm to rotate when character does
			CameraBoom->TargetArmLength = 800.f;
			CameraBoom->RelativeRotation = FRotator(-60.f, 0.f, 0.f);
			CameraBoom->bDoCollisionTest = false; // Don't want to pull camera in when it collides with level

			// Create a camera...
			TopDownCameraComponent = NewObject<UCameraComponent>(this, UCameraComponent::StaticClass());
			TopDownCameraComponent->RegisterComponent();
			TopDownCameraComponent->AttachTo(CameraBoom, USpringArmComponent::SocketName);
			TopDownCameraComponent->bUsePawnControlRotation = false;
		}
}

When you possess the pawn becomes view target, camera system then searches view target for camera component inside the view target pawn. But because you creating components in controller, camera component is registered in controller not in your Pawn and camera system don’t see camera component. You should set up camera component in pawn, when you possess pawn it becomes view target and that actor manages camera which by default follows camera component inside actor.

You can change that behavior if you wish, i explained how camera system works in UE4 many times so instead of typing this over and over again i will simply paste a link from other question (but it seems you find it already ^^):

Problem being that if I want to implement something like mind control, I need to implement a camera and a spring arm for each of the existing character in game, that doesn’t seems that good…
I saw your earlier comments on that, and I tried looking through all those things and unreal source, but at this point it is to much to grasp :slight_smile:

Thanks for explanation, things are now a bit easier to understand.
You’re sure the object is registered to controller in RegisterComponent ? I will look tomorrow more into it. Would changing this help:

CameraBoom = NewObject<USpringArmComponent>(InPawn, USpringArmComponent::StaticClass());

Or is there a way to register a component into a different object. Maybe even something crazy like making an interface:

//  Called from controller to register into the character
MyCharacter->Register(USpringArmComponent* arm);

I don’t know, just tired and a lot of crazy ideas…

Anyway, thanks for your help :slight_smile:

It worked!

CameraBoom = NewObject<USpringArmComponent>(InPawn, TEXT("CameraBoom"));

It seems that the owned Actor is set here, but not in RegisterComponent, and finally it works ! And the “Outer object” is simply the owner of the created Object ?

I still have a question, I am building onto the TopDown template, and the spring arm with the camera won’t rotate into position until I click to move the character.

Then declere camera components in base class of pawn that can be mind controlled, or for all characters overall. You really don’t need to do that dynamicly

Check if there some code in TopDown character that might be involved