Apply Damage

This commit is contained in:
2025-09-29 08:51:39 -04:00
parent d144e13084
commit 4adbb38f27
5 changed files with 21 additions and 1 deletions

Binary file not shown.

View File

@@ -45,7 +45,8 @@ void ABasePawn::Fire()
FRotator Rotation = ProjectileSpawnPoint->GetComponentRotation(); FRotator Rotation = ProjectileSpawnPoint->GetComponentRotation();
GetWorld()->SpawnActor<AProjectile>(ProjectileClass, Location, Rotation); auto Projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileClass, Location, Rotation);
Projectile->SetOwner(this);
/*DrawDebugSphere( /*DrawDebugSphere(
GetWorld(), GetWorld(),

View File

@@ -36,6 +36,9 @@ void UHealthComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActo
void UHealthComponent::DamageTaken(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* Instigator, AActor* DamageCauser) void UHealthComponent::DamageTaken(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* Instigator, AActor* DamageCauser)
{ {
if (Damage <= 0.f) return;
Health -= Damage;
UE_LOG(LogTemp, Warning, TEXT("Health left: %f"), Health);
} }

View File

@@ -4,6 +4,9 @@
#include "Projectile.h" #include "Projectile.h"
#include "Components/StaticMeshComponent.h" #include "Components/StaticMeshComponent.h"
#include "GameFramework/ProjectileMovementComponent.h" #include "GameFramework/ProjectileMovementComponent.h"
#include "GameFramework/DamageType.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/DamageEvents.h"
// Sets default values // Sets default values
AProjectile::AProjectile() AProjectile::AProjectile()
@@ -40,6 +43,16 @@ void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimi
UE_LOG(LogTemp, Warning, TEXT("OtherActor: %s"), *OtherActor->GetName()); UE_LOG(LogTemp, Warning, TEXT("OtherActor: %s"), *OtherActor->GetName());
UE_LOG(LogTemp, Warning, TEXT("OtherComp: %s"), *OtherComp->GetName());*/ UE_LOG(LogTemp, Warning, TEXT("OtherComp: %s"), *OtherComp->GetName());*/
auto MyOwner = GetOwner();
if (MyOwner == nullptr) return;
auto MyOwnerInstigator = MyOwner->GetInstigatorController();
auto DamageTypeClass = UDamageType::StaticClass();
if (OtherActor && OtherActor != this && OtherActor != MyOwner)
{
UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwnerInstigator, this, DamageTypeClass);
Destroy();
}
} }

View File

@@ -30,6 +30,9 @@ private:
UFUNCTION() UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit); void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
UPROPERTY(EditAnywhere);
float Damage = 50.f;
public: public:
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;