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:
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);
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.
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.