Implementing the USpringArmComponent and UCameraComponent from C++

.h:

UPROPERTY(EditAnywhere, Category = "Camera"))
class USpringArmComponent* SpringArmComponent;
UPROPERTY(EditAnywhere, Category = "Camera")
class UCameraComponent* CameraComponent;

.cpp:

#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"

//Within the constructor:

SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
SpringArmComponent->SetupAttachment(GetRootComponent());
SpringArmComponent->SetRelativeLocation(FVector(0, 0, 70));
SpringArmComponent->TargetArmLength = 300;
SpringArmComponent->bUsePawnControlRotation = true;

CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
CameraComponent->SetupAttachment(SpringArmComponent, USpringArmComponent::SocketName); 
CameraComponent->bUsePawnControlRotation = false;
2 Likes

Privacy & Terms