From 8d4c4bc0bd13657d3f7b66b148b39d01f008d41a Mon Sep 17 00:00:00 2001 From: baz Date: Thu, 27 Jun 2024 20:57:43 +0100 Subject: [PATCH] Move enemies away from player when taking damage --- Source/vampires/PlayerCharacter.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Source/vampires/PlayerCharacter.cpp b/Source/vampires/PlayerCharacter.cpp index 177f776..f5dd92b 100644 --- a/Source/vampires/PlayerCharacter.cpp +++ b/Source/vampires/PlayerCharacter.cpp @@ -115,25 +115,33 @@ void APlayerCharacter::OnGarlicEndOverlap(UPrimitiveComponent* OverlappedComp, A } void APlayerCharacter::GarlicUpdate() -{ - for (int i = 0; i < OverlappedEnemies.Num(); i++) +{ + TArray OverlappedEnemiesCache = OverlappedEnemies; + + for (int i = 0; i < OverlappedEnemiesCache.Num(); i++) { bool deadCheck = false; - UHealthComponent* EnemyHealthComponent = OverlappedEnemies[i]->GetHealthComponent(); + UHealthComponent* EnemyHealthComponent = OverlappedEnemiesCache[i]->GetHealthComponent(); if (!EnemyHealthComponent->GetIsDead()) { + FVector Direction = OverlappedEnemiesCache[i]->GetActorLocation() - this->GetActorLocation(); + Direction.Normalize(); + float distance = GarlicSphereComponent->GetScaledSphereRadius(); + Direction *= distance; + OverlappedEnemiesCache[i]->SetActorLocation(OverlappedEnemiesCache[i]->GetActorLocation() + Direction); + if (EnemyHealthComponent->GetCurrentHealth() < GarlicDamage) { deadCheck = true; } - EnemyHealthComponent->TakeDamage(OverlappedEnemies[i], GarlicDamage, nullptr, GetController(), this); + EnemyHealthComponent->TakeDamage(OverlappedEnemiesCache[i], GarlicDamage, nullptr, GetController(), this); } if (deadCheck) { i -= 1; } - } + } }