Unreal Engine 5.6 Tutorial 13 (Aug 2025) | Smooth Melee Combat with GAS




Adjusting Position & Rotation + Closest Enemy Targeting in UE5 (GAS, Blueprint)

In this tutorial we’re improving our light attack ability so that combat feels responsive, natural, and consistent. The system does four things:

Finds the closest enemy, with a preference for those in front.

Rotates the attacker to face that enemy before the hit connects.

Snaps position forward if we’re slightly out of range (to avoid “whiff” attacks).

Chooses the correct attack animation (left → right or right → left) depending on which side the enemy is on.

All of this works in multiplayer with proper replication.

Why we’re doing this

Attacks feel bad if the player doesn’t face their target.

It’s frustrating to miss just because you’re a few units too far away.

Left/right swings should match the side of the enemy so animations look natural.

Snapping and rotation are common in modern action games (e.g. God of War, DMC) to sell the impact.

Step 1 — Find Closest Enemy

We add a helper function GetClosestEnemy() in our parent Gameplay Ability:

Use SphereOverlapActors with:

Center: ActorLocation + ForwardVector * 50 (biased in front).

Radius: 250 units.

Object Type: Pawn.

Ignore: self.

From the overlapped pawns, pick the one with the smallest distance to a point in front of the player.
Return that actor as ClosestEnemy.

Step 2 — Detect Left or Right

We also need to know if the enemy is on our left or right:

Compute points slightly to the right and left of the front point.

Measure distance from the enemy to each.

If it’s closer to the left point → treat as left.

If it’s closer to the right → treat as right.

Add a small bias so ties default to the common right-to-left swing.

Step 3 — Adjust Position & Rotation

Rename our helper to AdjustPositionAndRotation() because it now does both:

Position snap

Check distance to the enemy.

If Greater than 200 units:

Find the direction from enemy → player.

Move the player to a spot 190 units away from the enemy.

This keeps the attacker inside hit range.

Rotation

Use FindLookAtRotation(MyLoc, EnemyLoc).

Apply Yaw only to keep the character upright.

Step 4 — Play Correct Animation

Now that we know which side the enemy is on:

If enemy is on left → play Right→Left swing montage.

If enemy is on right → play Left→Right swing montage.

Use a Select node to pick the montage before playing.

Step 5 — Test in Multiplayer

Run with one server + one client. Confirm that:

The attacker rotates correctly.

Snapping happens on both server and client.

Correct animation plays for the enemy’s side.

Hits register (VFX/“blood trail” shows).

Tweakable Values

Search Radius: 250 (was 300).

Snap Threshold: 200 (when to move forward).

Target Distance: 190 (where to stop near enemy).

Side Offset: 100 (used in left/right check).

Tie Bias: -10 (defaults to right-to-left when distances are close).

Final Result

With these changes:

The character always faces the target.

Attacks never whiff just because you’re a little too far.

Swing direction matches enemy position, making combat look natural.

Everything replicates correctly in multiplayer.

This creates smooth, satisfying melee combat while keeping the Blueprint logic clean and reusable for other abilities.

#UE5 #UnrealEngine #UnrealEngine5 #Blueprint #GameplayAbilitySystem #GAS #GameDev #GameDevelopment #IndieDev #UE5Tutorial #UnrealTutorial #ActionCombat #MeleeCombat #Replication #MultiplayerGames #AnimationBlueprint

Voir sur youtube

KA2Studio
Logo
Compare items
  • Total (0)
Compare
0