Download

How do I set the default camera on a map?

My goal is to create a starting map. I have a pawn blueprint in a blank project. I have attempted to put a Camera object in the same map facing the pawn, but I cannot seem to set it as the default view. I’ve been looking through the Beginner tutorials to try and find a way to set the camera, but it’s not looking like I easily can.

This is what I see in the editor, but when I compile and play, the camera resets to the center and I can’t see the player.

Can anyone point me to some C++ code or Project Settings tutorials on how to set the camera so what I see in the editor matches what I see when I play?

on the APlayerController class, use SetViewTargetWithBlend

I interpret this to mean that I should create a subclass of PlayerController and use a blueprint of that in the Project settings? Or is there a simpler way? My goal is to edit this project entirely in C++ to what degree I can.

you can get the controller from the pawn that you are controlling locally

Cast<APlayerController>(GetController())->SetViewTargetWithBlend();

Okay, well that seems to get me one step closer, but now I have a new problem. Seems that the CameraComponent I’m trying to set as the master camera can’t be accepted as an argument for SetViewTarget().

Here is the code I have presently in the constructor of my player object:

    //Create a third person camera component
	ThirdPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("ThirdPersonCamera"));
	check(ThirdPersonCameraComponent != nullptr);

	//Position the camera about 5 meters away from the bird, sideways
	ThirdPersonCameraComponent->SetRelativeLocation(FVector(CAMERA_OFFSET_X, CAMERA_OFFSET_Y, CAMERA_OFFSET_Z));

	//Rotate the camera to face the player, and so that player appears to face to the right.
	ThirdPersonCameraComponent->SetRelativeRotation(FRotator(0.0f, 0.0f, CAMERA_ROTATE));

	//Set the view to the player's third person camera
	Cast<APlayerController>(GetController())->SetViewTarget(ThirdPersonCameraComponent);
	//^^^The above line is raising an error because it apparently wants an AActor* and not a Camera.

Any ideas as to where I’m going wrong here?

its because you’re using CameraComponent instead of CameraActor.

See in your screenshot you posted, in the world outliner, it says “CameraActor”

That function only accepts actors

Okay, I slept on this after tinkering with the ACameraActor class as well. I understand that SetViewTarget() only takes Actors as arguments, whereas SetRelativeLocation() and SetRelativeRotation() take CameraComponent arguments.

So this is what I tried next. The result is that whenever I try to compile the code, Unreal Engine crashes, and I can’t reopen the project successfully without commenting out the SetViewTarget() line.

    //Create a third person camera component
	ThirdPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("ThirdPersonCamera"));
	check(ThirdPersonCameraComponent != nullptr);

	//Position the camera about 5 meters away from the bird, sideways
	ThirdPersonCameraComponent->SetRelativeLocation(FVector(CAMERA_OFFSET_X, CAMERA_OFFSET_Y, CAMERA_OFFSET_Z));
	
	//Rotate the camera to face the player, and so that player appears to face to the right.
	ThirdPersonCameraComponent->SetRelativeRotation(FRotator(0.0f, 0.0f, CAMERA_ROTATE));
	
	//Set the view to the player's third person camera
	//Cast<APlayerController>(GetController())->SetViewTarget(this);

[EDIT] I should specify that “this” is a subclass of a Pawn, because I want this class to be the player controlled object. Based on the documentation, I was to believe that the Pawn class is a child class of the Actor class. Am I mistaken?

i think it’s because your ViewTarget is already set to “this” by default when the pawn is possessed by the controller.

If you want to move your camera, you can either move the existing camera, or get a reference to some other CameraActor and use SetViewTarget(), but you can’t use SetViewTarget on yourself when the game starts

First of all, thank you so much for sticking with me while I work this out. I really do appreciate the help.

So now what I’ve done is I have recreated my blueprint of my player character, which has a CameraComponent property now. After compiling, this is what is shown in the viewport. It’s closer to what I want, but for some reason, the camera is not turned the 270 degrees to face the player (or does it face the player by default and I’m screwing it up?)

I expect that when I compile and run the game, this may be why I don’t see the bird in my view. What say you?

its no problem, i learn a lot by helping others.

The problem here is that your mesh is attached to the camera, and it should be the other way around.

You should attach your camera to the mesh, then move the camera, so that way, your mesh stays still when you move the camera

Okay, that makes sense. So at present, this is what I have in my .h file:

    // Third Person Camera
	UPROPERTY(VisibleAnywhere)
		UCameraComponent* ThirdPersonCameraComponent;
	
	//Mesh (eagle) 
	UPROPERTY(VisibleAnywhere, Category = Mesh)
		USkeletalMeshComponent* PlayerMesh;

And this is the part in the constructor in the .cpp file.

    //Create a third person camera component
	ThirdPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("ThirdPersonCamera"));
	check(ThirdPersonCameraComponent != nullptr);

	//Position the camera about 5 meters away from the bird, sideways
	ThirdPersonCameraComponent->SetRelativeLocation(FVector(CAMERA_OFFSET_X, CAMERA_OFFSET_Y, CAMERA_OFFSET_Z));
	
	//Rotate the camera to face the player, and so that player appears to face to the right.
	ThirdPersonCameraComponent->SetRelativeRotation(FRotator(0.0f, 0.0f, CAMERA_ROTATE));
	
	//Set the view to the player's third person camera
	//Cast<APlayerController>(GetController())->SetViewTarget(this);
	
	//Create a first-person mesh component for the owning player.
	PlayerMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("PlayerMesh"));
	check(PlayerMesh != nullptr);

How do I attach the mesh to my player instead of the camera? Is it a matter of the order of these commands? Or is there something else I’m missing?

make your camera the root like this:

RootComponent = PlayerMesh;

then attach your camera with

ThirdPersonCameraComponent->SetupAttachment(PlayerMesh);

you can check out Character.cpp and see how they do it there and maybe find some other neat stuff

Excellent! Thank you so much for your assistance. Now I can work on making the pawn actually move later on.