#include "RotatingActor.h" URotatingActor::URotatingActor() { PrimaryComponentTick.bCanEverTick = true; } void URotatingActor::BeginPlay() { Super::BeginPlay(); AudioComponent = GetOwner()->FindComponentByClass(); if (!AudioComponent) { UE_LOG(LogTemp, Error, TEXT("No audio component found on: %s !"), *GetOwner()->GetName()); } } void URotatingActor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); if (rotating) { FRotator Rotation = GetOwner()->GetActorRotation(); if (Rotation.Equals(targetRotation, 1.0f)) { rotating = false; }else { Rotation.Add(0.0f, DeltaTime * RotationSpeed * tempRotation, 0.0f); GetOwner()->SetActorRotation(Rotation); } } } void URotatingActor::Rotate(const FRotator TargetRotation) { this->targetRotation = TargetRotation; rotating = true; tempRotation = (TargetRotation.Yaw < 0) ? -1.f : 1.f; if(Negative) { tempRotation = (TargetRotation.Yaw < 0) ? 1.f : -1.f; } if(AudioComponent != nullptr && !soundHasBeenPlayed) { AudioComponent->Play(); soundHasBeenPlayed = true; } } void URotatingActor::Rotate() { FRotator rotation = GetOwner()->GetActorRotation(); rotation.Yaw = TargetAngle; Rotate(rotation); }