Update Projectile to use a ProjectileMovementComponent

This commit is contained in:
baz 2024-11-13 23:39:08 +00:00
parent fc93e35ef1
commit 4fa8210707
10 changed files with 37 additions and 27 deletions

BIN
Content/Player/BP_PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

View File

@ -3,17 +3,20 @@
#include "Projectile.h"
#include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Weapons/ProjectileWeapon.h"
// Sets default values
AProjectile::AProjectile()
{
// 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;
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
{ SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SetRootComponent(SphereComponent);
SphereComponent->SetSphereRadius(50.0f);
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement"));
ProjectileMovement->ProjectileGravityScale = 0.0f;
ProjectileMovement->Friction = 0.0f;;
ProjectileMovement->bIsSliding = true;
}
// Called when the game starts or when spawned
@ -22,12 +25,14 @@ void AProjectile::BeginPlay()
Super::BeginPlay();
AProjectileWeapon* OwnerWeapon = Cast<AProjectileWeapon>(GetOwner());
SphereComponent->OnComponentBeginOverlap.AddDynamic(OwnerWeapon, &AProjectileWeapon::OnProjectileBeginOverlap);
ProjectileMovement->InitialSpeed = ProjectileSpeed;
ProjectileMovement->MaxSpeed = ProjectileSpeed;
}
// Called every frame
void AProjectile::Tick(float DeltaTime)
void AProjectile::SetTargetDirection(FVector direction)
{
Super::Tick(DeltaTime);
SetActorLocation(GetActorLocation() + (TargetDirection * ProjectileSpeed));
TargetDirection = direction;
ProjectileMovement->SetVelocityInLocalSpace(TargetDirection * ProjectileSpeed);
}

View File

@ -3,10 +3,12 @@
#pragma once
#include "CoreMinimal.h"
#include "Components/SphereComponent.h"
#include "GameFramework/Actor.h"
#include "Projectile.generated.h"
class UProjectileMovementComponent;
class USphereComponent;
UCLASS()
class VAMPIRES_API AProjectile : public AActor
{
@ -16,10 +18,13 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
USphereComponent* SphereComponent = nullptr;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UProjectileMovementComponent* ProjectileMovement = nullptr;
FVector TargetDirection = FVector::ZeroVector;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float ProjectileSpeed = 1.0f;
float ProjectileSpeed = 500.0f;
public:
// Sets default values for this actor's properties
@ -30,6 +35,6 @@ protected:
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void SetTargetDirection(FVector direction);
};

View File

@ -34,6 +34,6 @@ void AFireWandWeapon::FireWeaponAction_Implementation()
direction.Z = 0.0;
direction.Normalize();
projectile->TargetDirection = direction;
projectile->SetTargetDirection(direction);
}
}

View File

@ -80,7 +80,7 @@ void AGarlicWeapon::GarlicFireWeaponAction(FOverlappedEnemy EnemyCharacter)
}
EnemyHealthComponent->TakeDamage(EnemyCharacter.OverlappedEnemyCharacter, Damage, nullptr,
ownerController, this);
ownerController, this);
}
}
}

View File

@ -12,11 +12,11 @@ USTRUCT()
struct FOverlappedEnemy
{
GENERATED_BODY()
AEnemyCharacter* OverlappedEnemyCharacter;
FTimerHandle OverlappedTimerHandle;
};
};
/**
*

View File

@ -53,18 +53,18 @@ void AGunWeapon::FireWeaponAction_Implementation()
AProjectile* projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopLeft);
projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopLeft));
projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopRight);
projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopRight));
projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomLeft);
projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomLeft));
projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomRight);
projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomRight));
}
}

View File

@ -32,6 +32,6 @@ void AKnifeWeapon::FireWeaponAction_Implementation()
FVector direction = FVector(playerCharacter->PreviousMovementDirection, 0.0);
direction.Normalize();
projectile->TargetDirection = direction;
projectile->SetTargetDirection(direction);
}
}

View File

@ -37,7 +37,7 @@ void AMagicWandWeapon::FireWeaponAction_Implementation()
GetActorLocation(), nearestActor->GetActorLocation());
direction.Z = 0.0;
direction.Normalize();
projectile->TargetDirection = direction;
projectile->SetTargetDirection(direction);
}
}
}