From 5e0433905f236b1f425760bbee94486ea0f4cec8 Mon Sep 17 00:00:00 2001 From: baz Date: Sat, 9 Mar 2024 03:52:14 +0000 Subject: [PATCH] Rotate Healthbar to face Player Camera in Tick --- Source/Nakatomi/EnemyCharacter.cpp | 19 +++++++++++++++++++ Source/Nakatomi/EnemyCharacter.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/Source/Nakatomi/EnemyCharacter.cpp b/Source/Nakatomi/EnemyCharacter.cpp index 3bef9c6..2efa28f 100644 --- a/Source/Nakatomi/EnemyCharacter.cpp +++ b/Source/Nakatomi/EnemyCharacter.cpp @@ -8,6 +8,8 @@ #include "BehaviorTree/BehaviorTree.h" #include "BehaviorTree/BlackboardComponent.h" #include "BehaviorTree/BlackboardData.h" +#include "Kismet/GameplayStatics.h" +#include "Kismet/KismetMathLibrary.h" #include "UI/EnemyHealthbarUserWidget.h" #define COLLISION_WEAPON ECC_GameTraceChannel1 @@ -78,6 +80,23 @@ void AEnemyCharacter::BeginPlay() } } +void AEnemyCharacter::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + + APlayerCharacter* PlayerCharacter = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); + if (PlayerCharacter && HealthbarWidgetComponent->GetWidget()) + { + FVector ActorLocation = GetActorLocation(); + + UCameraComponent* PlayerCamera = PlayerCharacter->GetCameraComponent(); + FVector PlayerLocation = PlayerCamera->GetComponentTransform().GetLocation(); + + FRotator rotation = UKismetMathLibrary::FindLookAtRotation(ActorLocation, PlayerLocation); + HealthbarWidgetComponent->SetWorldRotation(rotation); + } +} + void AEnemyCharacter::PlayOnFireAnimations() { Super::PlayOnFireAnimations(); diff --git a/Source/Nakatomi/EnemyCharacter.h b/Source/Nakatomi/EnemyCharacter.h index 8b7bd08..545a6f6 100644 --- a/Source/Nakatomi/EnemyCharacter.h +++ b/Source/Nakatomi/EnemyCharacter.h @@ -53,6 +53,9 @@ public: protected: virtual void BeginPlay() override; +public: + virtual void Tick(float DeltaTime) override; + private: virtual void PlayOnFireAnimations() override;