Files
Yamato/Source/MyProject3/CameraFollowActor.cpp
2025-02-13 00:03:56 +05:30

34 lines
971 B
C++

// 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<UCameraComponent>(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);
}
}