0% found this document useful (0 votes)
14 views3 pages

Weapon Hit System Improvements

Uploaded by

ipsiprtn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Weapon Hit System Improvements

Uploaded by

ipsiprtn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Unreal Engine - Weapon Trace Hit System Documentation

======================================================

📝 Handwritten Note Summary (from screenshot)


----------------------------------------------
Topic: To Ignore Sword After It Hits

Summary:
- In UKismet box tracing, there is a TArray to ignore actors.
- After the first hit, we can add the actor to the ignore list.
- Problem: If we don't reset it, the actor will never get hit again.
- Solution: Empty the ignore list before a new attack begins.

------------------------------------------------------

📦 Problem Recreated in Code


----------------------------

Original (Incorrect or Redundant) Code:

for (AActor* Actor : ActorsToIgnore) {


[Link](Actor); // ❌ No effect — redundant
}

🧠 Why this is redundant:


- You're iterating over a list and trying to re-add what’s already inside.
- Has no change or effect, especially if using AddUnique().
- Instructor may have used it, but in context, it has no purpose.

------------------------------------------------------

✅ Final Working Code Snippets


------------------------------

1. 🔁 OnBoXOverlap Function:
----------------------------
void AWeapon::OnBoXOverlap(UPrimitiveComponent* OverlappedComponent, AActor*
OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const
FHitResult& SweepResult)
{
AMainCharacter* mc =
Cast<AMainCharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));
if (!mc || mc->getActionState() != EActionState::EAS_Attack) return;

const FVector StartTraceLoc = BoxTraceStart->GetComponentLocation();


const FVector EndTraceLoc = BoxTraceEnd->GetComponentLocation();

[Link](this); // Ignore self

FHitResult BoxHit;
UKismetSystemLibrary::BoxTraceSingle(
this,
StartTraceLoc,
EndTraceLoc,
FVector(5.f, 5.f, 5.f),
BoxTraceStart->GetComponentRotation(),
ETraceTypeQuery::TraceTypeQuery1,
false,
ActorsToIgnore,
EDrawDebugTrace::ForDuration,
BoxHit,
true
);

if ([Link]())
{
IHitInterface* HitInterface = Cast<IHitInterface>([Link]());
if (HitInterface) {
HitInterface->GetHit([Link]);
}
[Link]([Link]());
}
}

2. 🔁 Reset Ignore List Before Attack:


-------------------------------------
void AMainCharacter::SetWeaponCollision(ECollisionEnabled::Type CollisionEnable)
{
if (EquipW && EquipW->getWeaponBox()) {
EquipW->getWeaponBox()->SetCollisionEnabled(CollisionEnable);
EquipW->[Link](); // ✅ Resetting hit list for new attack
}
}

------------------------------------------------------

🚫 Mistake in Tick() Usage


---------------------------
Originally, weapon hit detection was placed in the Tick() function:

void AWeapon::Tick(float DeltaTime)


{
AMainCharacter* mc =
Cast<AMainCharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));
if (EActionState::EAS_Attack == mc->getActionState()) {
// Do box trace and hit logic
}
}

❌ Why this was a mistake:


- Tick runs every frame (60+ times per second), even when not attacking.
- If `mc` is null or not in attack state, it risks crashing or wasting performance.
- Overlap + trace should be handled by the collision event (OnBoXOverlap), not per-
frame.

✅ Corrected:
- All tracing is now done inside `OnBoXOverlap`, which only runs when a hit
happens.
- Tick function is no longer responsible for attack tracing, making the system
cleaner and safer.

------------------------------------------------------

📘 Final Notes:
---------------
- Your fix by moving logic out of Tick() and into overlap event was correct and
smart.
- Your removal of the redundant loop shows deeper understanding than just copying
tutorials.
- This system is now efficient, safe, and ready for combat logic expansion (e.g.,
combos, special effects, etc.)

🔥 Keep going! You've got solid instincts for debugging and refactoring Unreal
Engine C++ code.

You might also like