Add Bouncing Static Mesh to Health Pickup

This commit is contained in:
Louis Hobbs 2023-07-04 22:22:27 +01:00
parent df9a1af227
commit db22a4eb58
3 changed files with 41 additions and 1 deletions

View File

@ -1,4 +1,5 @@
#include "HealthPickup.h"
#include "HealthPickup.h"
#include "PlayerCharacter.h"
@ -7,18 +8,35 @@ AHealthPickup::AHealthPickup()
{
// 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;
StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
StaticMeshComponent->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
void AHealthPickup::BeginPlay()
{
Super::BeginPlay();
StaticMeshComponent->SetWorldLocation(this->GetActorLocation());
StartingLocation = this->GetActorLocation();
StaticMeshComponent->SetCollisionProfileName(FName("NoCollision"));
}
// Called every frame
void AHealthPickup::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (StaticMeshComponent)
{
// Rotate Weapon in desired direction
StaticMeshComponent->AddLocalRotation((SpinRotation * RotationSpeed) * DeltaTime);
// Bob weapon up and down
const float Time = GetWorld()->GetRealTimeSeconds();
const float Sine = FMath::Abs(FMath::Sin(Time * MovementSpeed));
StaticMeshComponent->SetRelativeLocation(StartingLocation + ((MovementDirection * Sine) * MovementDistance));
}
}
void AHealthPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,

View File

@ -12,6 +12,28 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float Health = 20.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UStaticMeshComponent* StaticMeshComponent = nullptr;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
FVector MovementDirection = FVector(0.0f, 0.0f, 1.0f);
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float MovementDistance = 1.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float MovementSpeed = 1.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
FRotator SpinRotation = FRotator(0.0, 1.0f, 0.0f);
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float RotationSpeed = 50.0f;
private:
UPROPERTY()
FVector StartingLocation;
public:
// Sets default values for this actor's properties
AHealthPickup();

View File

@ -31,7 +31,7 @@ void AWeaponPickup::Tick(const float DeltaTime)
// Bob weapon up and down
const float Time = GetWorld()->GetRealTimeSeconds();
const float Sine = FMath::Sin(Time * MovementSpeed);
const float Sine = FMath::Abs(FMath::Sin(Time * MovementSpeed));
WeaponComponent->SetActorLocation(WeaponStartingLocation + ((MovementDirection * Sine) * MovementDistance));
}
}