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

View File

@ -3,10 +3,12 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "Components/SphereComponent.h"
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "Projectile.generated.h" #include "Projectile.generated.h"
class UProjectileMovementComponent;
class USphereComponent;
UCLASS() UCLASS()
class VAMPIRES_API AProjectile : public AActor class VAMPIRES_API AProjectile : public AActor
{ {
@ -16,10 +18,13 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
USphereComponent* SphereComponent = nullptr; USphereComponent* SphereComponent = nullptr;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UProjectileMovementComponent* ProjectileMovement = nullptr;
FVector TargetDirection = FVector::ZeroVector; FVector TargetDirection = FVector::ZeroVector;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
float ProjectileSpeed = 1.0f; float ProjectileSpeed = 500.0f;
public: public:
// Sets default values for this actor's properties // Sets default values for this actor's properties
@ -30,6 +35,6 @@ protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
public: 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.Z = 0.0;
direction.Normalize(); 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, EnemyHealthComponent->TakeDamage(EnemyCharacter.OverlappedEnemyCharacter, Damage, nullptr,
ownerController, this); ownerController, this);
} }
} }
} }

View File

@ -53,18 +53,18 @@ void AGunWeapon::FireWeaponAction_Implementation()
AProjectile* projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(), AProjectile* projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters); actorSpawnParameters);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopLeft); projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopLeft));
projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(), projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters); actorSpawnParameters);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopRight); projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopRight));
projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(), projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters); actorSpawnParameters);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomLeft); projectile->SetTargetDirection(UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomLeft));
projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(), projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters); 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); FVector direction = FVector(playerCharacter->PreviousMovementDirection, 0.0);
direction.Normalize(); direction.Normalize();
projectile->TargetDirection = direction; projectile->SetTargetDirection(direction);
} }
} }

View File

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