You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.8 KiB
68 lines
1.8 KiB
// 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();
|
|
}
|
|
|