// CameraFollowActor.cpp #include "CameraFollowActor.h" #include "Camera/CameraComponent.h" #include "GameFramework/Actor.h" // Sets default values ACameraFollowActor::ACameraFollowActor() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; // Create the camera component CameraComponent = CreateDefaultSubobject(TEXT("CameraComponent")); RootComponent = CameraComponent; } // Called when the game starts or when spawned void ACameraFollowActor::BeginPlay() { Super::BeginPlay(); } // Called every frame void ACameraFollowActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); if (TargetActor) { // Update the camera position to follow the target actor with the specified offset FVector NewLocation = TargetActor->GetActorLocation() + CameraOffset; SetActorLocation(NewLocation); } }