How to change default camera to CameraComponent

Hi,
I have following problem, I create CameraComponent in my base C++ class for Character

// Sets default values
ADefaultCharacter::ADefaultCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	defaultMovementSpeed = 600.0f;
	pCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera"));
}

// Called when the game starts or when spawned
void ADefaultCharacter::BeginPlay()
{
	Super::BeginPlay();
	if (pCamera) {
		pCamera->SetWorldLocation(FVector(-900, 100, 100));
		pCamera->Activate();
		APlayerController *pContr = GetWorld()->GetFirstPlayerController();
		if (pContr) {
			pContr->SetViewTargetWithBlend(this);
		}
	}
}

So far everything looks good - the component is created and assigned to object.
Now comes the tricky part. When I start game I can see there is some default camera created

64217-1.png

Now whenever my character moves I see camera standing still in place. But as I said, there is also CameraComponent in my Character

And after I click on DefaultCharacterBP40 the camera switches and now it is following the Character.

Question - how do I make that switch from the very beggining? I want my default Camera to be the one from CameraComponent. And how do I remove this one automaticaly added camera?

Hey -

I’m not sure I’m seeing the same result that you’re describing. I copied the code posted above into a custom Character class and created a BP of that class. When I set MyCharBP as the default pawn and play in editor the camera is located at the world origin. Could you post the full code for the character class so that I can make sure I’m following the same setup?

One thing that might help would be to attach the camera to another component (RootComponent/Mesh/SpringArm) so that the camera updates when its parent updates. Also, let me know if you have your custom character set to the default pawn inside the world settings.

Cheers

That is actually the full code, just header with declarations of functions and camera variable, nothing special.
But maybe some more explaination of how I set everything.
So I have two lvls, both using different GameModes. The first one is kind of “menu” lvl and is supposed to load 2nd world. The 2nd has blueprint GameMode which base class is in C++, I have also custom HUD class and player controller. There is also CameraManager class. Default Pawn Class is set to “none” because I am spawning it via C++ code from GameMode.

AMainGameMode::AMainGameMode() {
	static ConstructorHelpers::FObjectFinder<UClass> ItemBlueprint(TEXT("Class'/Game/DefaultCharacterBP.DefaultCharacterBP_C'"));
	if (ItemBlueprint.Object) {
		DefaultCharacterHandle = (UClass*)ItemBlueprint.Object;
	}
}

void AMainGameMode::BeginPlay()
{
	Super::BeginPlay();
	UWorld* const World = GetWorld();
	if (World) {
		DefaultCharacter = World->SpawnActor<ADefaultCharacter>(DefaultCharacterHandle);
		if (DefaultCharacter) {
			DebugLog("Character Spawned");
			DefaultCharacter->SetActorLocation(FVector(-900, 100, 40));
		}
	}
}

AMainPlayerController::AMainPlayerController() {
	DebugLog("PlayerController");
	bAutoManageActiveCameraTarget = false;
	PlayerCameraManagerClass = AMainPlayerCameraManager::StaticClass();
}

Other things (like my custom CameraManager) isn’t modified, just created and not modified.

When you start playing is the game view through the correct camera setup through the character or are you “looking” through the other camera that shows in the world outliner? If possible could you take a short video of what you’re seeing to help me visualize what’s happening.

Additionally, you may want to consider narrowing down what is returned to ItemBlueprint if you are trying to reference your character blueprint. With it set as a UClass it could return anything, instead it would be better to use APawn to ensure that the class it finds is a child of pawn (which characters are).

Here is the video.

On the BeginPlay for your character you can try calling [cameraComponent]->Activate(); to hopefully force your camera to be the primary. If that doesn’t work could you upload your project to dropbox and send me a link to download it. So far I’ve not had the same issue you’re seeing when starting the game and would like to test your problem directly.

Cheers

You didn’t attach the Camera to your actor.

pCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera"));
pCamera->AttachParent = RootComponent;

And if you just want to change your view target you can use ** SetViewTargetWithBlend **

pCamera->AttachParent = RootComponent;
Didn’t help

Here is packed project: Dropbox - Error - Simplify your life

I tested the project you linked and did not have the same behavior shown in your video. When I click the “Start Game” button and the level changes, the camera is attached to the actor as expect and will follow along with my movement inputs. I had noticed that in the code your pCamera is not set as a UPROPERTY() . The Spring Arm defined in code is also not being used. It may help to look at the Third Person Template character blueprint. There the SpringArm and Camera are added through the editor rather than defined in code (note that the “(inherited)” label is missing in the component list).

Hi ,

We have not heard back from you in a few days, so we are marking this post as Resolved for tracking purposes. If you are still experiencing the issue you reported, please respond to this message with additional information and we will offer further assistance.

Thank you.