UCameraComponent doesn't move with my player

Hello.
I’m making a game with custom Player Class and I came across a weird problem.
Basically everything worked fine until I added an UCameraComponent to my class, but when I added it my camera the view didn’t move. First I thought that my player doesn’t move, but then I discovered, it’s just the camera.
Here are all the references to my Camera Component:

	UPROPERTY(EditAnywhere)
	UCameraComponent* PlayerCamera;
..........
	PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
	PlayerCamera->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f));
	PlayerCamera->bUsePawnControlRotation = true;

I believe it’s something with the 5th line.
Should I use a different function? If yes then tell me what function.
Thanks.

Actually, when I tried this my UE crashed, and I couldn’t enter the project anymore, but I’ll try this again.

Are you actually attaching the camera to your player?

E.g.

PlayerCamera->SetupAttachment(RootComponent);

Hello !

This is how I do mine, in the constructor of my Pawn :

Commander.h

private:
	// Camera component for the Pawn
	UCameraComponent* camera;

	// Add a SpringArm for the Camera
	USpringArmComponent* springArm;

Commander.cpp

	// Create the springArm, camera and mesh of the commander.
	springArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
	camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));

	// Define the mesh as the root component.
	RootComponent = mesh;

	// Attach the springArm to the the mesh
	springArm->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);

	// Attach the camera to the springArm
	camera->AttachToComponent(springArm, FAttachmentTransformRules::KeepRelativeTransform);

Hope this helps !

What is this USpringArmComponent and what does it do?

Ok, thank you very much.

The SpringArmComponent is a spring that helps the camera doing smooth movements. Typically in a third person game, it will bring the camera closer when you are close to a wall.

See : - YouTube

You are not forced to use it, especially in a FPS game. All you need to do is to use the AttachToComponent using the object you want your camera to stick to as the first parameter.

So basically you will have something like

yourCamera->AttachToComponent(yourMesh, FAttachmentTransformRules::KeepRelativeTransform);