Video summary
Unreal Engine Hack & Slash Tutorial [2024] Series | Part 21 | Enemy AI Melee Combat
Main summary
Key takeaways
Main Ideas / Concepts Covered
- Building the first melee AI enemy in Unreal Engine:
- Enemies strafe around the player and then switch into melee attack behavior.
- Implementing a full melee attack pipeline:
- AI decides it can/should attack (behavior tree + blackboard/state checks).
- AI plays an attack montage and uses AnimNotifies to trigger weapon hit detection.
- Weapon hit detection applies damage only under the correct conditions.
- Handling common AI-control issues:
- Prevent attacks from being interrupted or repeatedly triggered while already attacking.
- Ensure Blackboard keys and “saved attack” flags are cleared/reset properly after attack animations.
- Tune timer/loop intervals so behavior tree state changes don’t conflict.
Methodology / Instruction-Style Steps
1) Start/Finish the Enemy “Melee Combat” Behavior
- Ensure the enemy already:
- Chases + strafes.
- Switches positions when the player is too far, or when search logic finds a new spot.
- Visually confirm attack timing:
- Enemies light up when an attack is about to occur.
- Attack indicators help confirm one enemy lights up while attacks are incoming.
2) Add Attack State Gating: can attack
- Use a custom event/function approach and a state-based check for gameplay consistency.
- Create a function such as
can attackthat returns whether the enemy is allowed to start an attack. - Intended state exclusions (examples):
- Enemy cannot attack if in any of these states:
- Attacking
- Dodging
- Dead
- Disabled
- (and possibly additional movement conditions like “flying” / “falling,” depending on implementation)
- Enemy cannot attack if in any of these states:
3) Implement Attack Playback Using Montages (Index-Based)
- Add an attack montage array (e.g.,
Attack montages array). - Implement logic such as
perform attack:- Accepts an attack index
- Validates the index
- If valid:
- Set enemy state to Attacking
- Play the selected attack montage
- Compile and confirm the montage plays when the attack is triggered.
4) Add Weapon Collision / Hit Detection via AnimNotify
- Add a weapon static mesh to the enemy and attach it to a weapon socket.
- Create a function like
Draw Weapon Collision(copied/adapted from player logic), taking inputs such as:radiusstart/endrange (trace sweep parameters)damage type class- target cast type (e.g., cast hit actors to the player BP)
- Behavior:
- Detect actors in the collision/trace area
- For each hit actor:
- Cast to the player character
- Check player state (e.g., not dead)
- Apply damage (set damage amount, apply selected damage type)
5) Trigger Collision During the Animation
- In the enemy’s attack montage:
- Add an AnimNotify at the right moment (e.g., “Draw Weapon Collision”).
- In the notify event, call the weapon collision function.
- Expose and tune notify collision parameters (example values mentioned):
ready≈ ~50end≈ ~150–250 (tuned experimentally)
6) Integrate Attack Decision into Behavior Tree Using Timers
- Use a timer approach instead of a full fight director.
- Create a loop timer in the AI controller/event graph:
- Uses Random Float in Range (roughly 3–8 seconds initially, later tuned)
- On completion, set a blackboard value to an attack/fight state (e.g.,
BehaviorStates = Fight)
- In the Behavior Tree:
- Give attack higher priority than tasks like strafing.
- Use abort settings so lower-priority branches stop when attack state becomes active.
7) Fix Repeated/Overridden Attacks (Critical Bug Handling)
Observed problems
- Attacks could trigger too many times or cancel too quickly because behavior tree reevaluations override state before montage completion.
- Delay-based task completion didn’t reliably prevent re-entry.
Fixes applied
- Add a “save attack” boolean key:
- Blackboard boolean like
B_SaveAttack
- Blackboard boolean like
- Logic intent:
- When attack conditions occur, store intent so it won’t be lost briefly due to re-evaluations.
- Attack task should only run when:
- Not already attacking
B_SaveAttackis set (or relevant conditions are true)
- Clear/reset:
- Clear
B_SaveAttack - Reset/clear the main state key after the montage ends
- Clear
- Result:
- Stabilizes behavior so the attack montage plays fully before another attack begins.
8) Implement Melee Combo Variation + Chance-Based Attacks
- Add an additional melee combo montage:
- e.g., enemy combo attack 1
- Include its own reset state notify/section
- Add a BT decorator for chance:
- Decorator checks:
random integer in range 0..100- If
<= chance, allow the combo task
- Decorator checks:
- Add cooldown after playing combo (example: 5 seconds mentioned).
- Adjust structure:
- Switch to a Selector instead of a Sequence to avoid deterministic “always combo then again” behavior (as noted in the video).
9) Improve Facing / Targeting During Attacks
- Reduce chaotic spinning by controlling focus:
- Use Set Focus during the attack moment
- Use Clear Focus at an appropriate time (often around end of animation timing)
10) Add Attack Indicators (Readability / Telegraphing)
- Add an overlay material signaling system:
- Custom event
Start Attack:- Set overlay material on enemy mesh
- Custom event
End Attack:- Clear overlay (set material to none)
- Custom event
- Wire indicator events to attack task:
- Start indicator when approaching/entering attack state
- End indicator after attack montage begins/after a delay
- Result:
- Players get clearer anticipation that an enemy is about to attack.
11) Prevent Taking Damage During Invulnerability Windows (i-frame Logic)
- Fix damage application so damage only occurs when i-frames are not enabled:
- In damage code, add a branch:
- If
iFrames enabled == true→ do not take damage - Else → allow damage
- If
- In damage code, add a branch:
- Result:
- The player no longer takes hits in scenarios where invulnerability should apply.
12) Final Tuning and Stress Test
- Stress test by spawning multiple enemies simultaneously.
- Adjust tuning values:
- Attack timer random ranges
- Behavior tree wait intervals during strafing
- Movement acceptance radius / approach radius (mentioned as being tuned toward ~50–600 depending on observed behavior)
- Notes:
- Combat can work but may become chaotic with many enemies colliding/spreading.
- Suggested future improvements:
- Check whether the target is in the air and back away or adjust positioning
- Improve collision avoidance between enemies
Speakers / Sources Featured
- Primary speaker (creator/instructor): “guys welcome back…” / tutorial narrator (no name given in subtitles)
- Video source: Unreal Engine hack & slash tutorial series, Part 21: Enemy AI Melee Combat (2024) (referenced by the video title)