How do I fix the height of my third person camera?

It took me a while to finally get to a point where I have my default pawn, and it has a camera component at the correct relative location to start and facing the right way.

In the game I am trying to create, however, I want the camera to be fixed, so it doesn’t move with the player as the player flaps up and down.

Based on the CameraDirector tutorial, I had thought I could instantiate a separate Camera actor in the room and have a pointer to that during my build, later using SetViewTarget(). However, when I try that approach, Unreal Engine crashes, and I have to recompile my old code before I can even open the project again.

I assume there must be a better way. Here is the *.cpp code I have at present, with the camera that moves up and down with the player. Remember, I want a FIXED camera, not a mobile one.

    //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(CAMERA_ROTATE_X, CAMERA_ROTATE_Y, CAMERA_ROTATE_Z));

	//Create a first-person mesh component for the owning player.
	PlayerMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("PlayerMesh"));
	check(PlayerMesh != nullptr);

	//Assign the mesh as the root component of the player object
	RootComponent = PlayerMesh;

	//Then attach the camera as an attachment

You could detach your camera component like this:


and then it would just float there when you detach.

You could also spawn a CameraActor with this and possess it with your controller, it shouldn’t crash. Either way is fine:

ACameraActor* PlayersCamera = GetWorld()->SpawnActor<ACameraActor>();

Thanks. Assuming I stick with the ThirdPersonCameraComponent option, do I still need this line in my .cpp constructor?


I’m assuming if I do, then I just put the first line you suggested directly underneath it.

You don’t need SetupAttachment, and you probably can’t detach in the contructor. You would just have to try it and see what happens.

I’m guessing you have to wait until BeginPlay() to detach

Thanks! Works great!

1 Like