// Copyright Voulz 2021-2025. All Rights Reserved. #include "FaceCameraComponent.h" #include "Engine/World.h" #include "Kismet/GameplayStatics.h" #include "Kismet/KismetMathLibrary.h" // Sets default values for this component's properties UFaceCameraComponent::UFaceCameraComponent() { PrimaryComponentTick.bCanEverTick = true; bTickInEditor = true; bAutoActivate = true; PrimaryComponentTick.bStartWithTickEnabled = true; } void UFaceCameraComponent::MakeActorFaceCamera() { #if WITH_EDITOR if (GetWorld() != nullptr && (GetWorld()->WorldType == EWorldType::Editor || GetWorld()->WorldType == EWorldType::EditorPreview)) { if (AActor* Owner = GetOwner()) { const FRotator Rotation = UKismetMathLibrary::FindLookAtRotation(GetEditorCameraLocation(), Owner->GetActorLocation()); Owner->SetActorRotation(FRotator(0.f, Rotation.Yaw, bAllowRoll ? Rotation.Pitch : 0.f) + AddedRotation); } } else { #endif if (AActor* Owner = GetOwner()) { if (const APlayerCameraManager* Manager = UGameplayStatics::GetPlayerCameraManager(GetWorld(), 0)) { const FRotator Rotation = UKismetMathLibrary::FindLookAtRotation(Manager->GetCameraLocation(), Owner->GetActorLocation()); Owner->SetActorRotation(FRotator(0.f, Rotation.Yaw, bAllowRoll ? Rotation.Pitch : 0.f) + AddedRotation); } } #if WITH_EDITOR }; #endif } // Called every frame void UFaceCameraComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); if (bFaceCameraActive) { MakeActorFaceCamera(); } } FVector UFaceCameraComponent::GetEditorCameraLocation() const { if (const UWorld* World = GetWorld()) { if (World->ViewLocationsRenderedLastFrame.Num() > 0) { return World->ViewLocationsRenderedLastFrame[0]; } } return FVector(); }