#include "MovingActor.h" #include "Components/AudioComponent.h" #include "Kismet/GameplayStatics.h" UMovingActor::UMovingActor() { PrimaryComponentTick.bCanEverTick = true; } void UMovingActor::BeginPlay() { Super::BeginPlay(); } void UMovingActor::Move() { targetLocation = GetOwner()->GetActorLocation(); targetLocation.X += MovementTarget; tempLocation = FVector(MovementSpeed, 0.0f, 0.0f); if (Negative) { targetLocation.X -= MovementTarget; targetLocation.Y += MovementTarget; tempLocation = FVector(0.0f, MovementSpeed, 0.0f); } this->targetLocation = targetLocation; this->moving = true; TArray Audios; GetOwner()->GetComponents(Audios); if (Audios.Num() == 0) return; if (soundHasBeenPlayed) return; for (auto Audio : Audios) { if (Audio->ComponentHasTag("location")) { UGameplayStatics::PlaySoundAtLocation(GetOwner(), Audio->Sound, GetOwner()->GetActorLocation(), GetOwner()->GetActorRotation(), 1, 1, 0, Audio->AttenuationSettings, nullptr, GetOwner()); } else { Audio->Play(); } } soundHasBeenPlayed = true; } void UMovingActor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); if (moving) { FVector Location = GetOwner()->GetActorLocation(); if (Location.Equals(targetLocation, 1.0f)) { moving = false; } else GetOwner()->SetActorLocation(Location + tempLocation); } }