Compare commits

...

2 Commits

Author SHA1 Message Date
baz e8d7c49a4e Add Pickup Movement 2024-06-24 21:31:42 +01:00
baz 97201d8b6a Make OnBeginOverlap a UFUNCTION() 2024-06-24 21:21:32 +01:00
2 changed files with 26 additions and 5 deletions

View File

@ -4,12 +4,13 @@
#include "Pickup.h"
#include "PlayerCharacter.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
APickup::APickup()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Create Sphere Component
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
@ -29,10 +30,22 @@ void APickup::Tick(float DeltaSeconds)
Super::Tick(DeltaSeconds);
// TODO: Move actor towards player when in range
FVector actorLocation = GetActorLocation();
FVector playerLocation = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)->GetActorLocation();
double currentDistance = FVector::Distance(actorLocation, playerLocation);
if (currentDistance <= PickupMovementRange)
{
double speed = 400 / currentDistance;
UE_LOG(LogTemp, Warning, TEXT("The integer value is: %f"), speed);
FVector location = FMath::VInterpTo(actorLocation, playerLocation, DeltaSeconds, speed);
SetActorLocation(location);
}
}
void APickup::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (APlayerCharacter* PlayerCharacter = Cast<APlayerCharacter>(OtherActor))
{
@ -40,4 +53,3 @@ void APickup::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* O
Destroy();
}
}

View File

@ -12,6 +12,14 @@ class VAMPIRES_API APickup : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
double PickupMovementRange = 500;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
double PickupMovementSpeed = 1000;
private:
UPROPERTY()
USphereComponent* SphereComponent;
@ -27,6 +35,7 @@ protected:
public:
virtual void Tick(float DeltaSeconds) override;
UFUNCTION()
virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);