Lock-On Targeting in UE5: Switch Targets and Steer Attacks

Documentation Unreal Engine Player Combat Targeting Tutorial

Souls-like lock-on for Unreal Engine 5 in Blueprint: lock the enemy you look at, switch with a flick, keep the camera on it, and turn each attack to the target.


Lock-on is three jobs: pick the right enemy when the button goes down, move to the next one with a flick of the stick, and make each attack land on the locked enemy instead of the air beside it. Universal Player Combat does the first two in its Targeting component and the third in Attack Steering, which turns each attack toward its target and glides it into range.
Both parts run on Unreal Engine 5.6, 5.7 and 5.8, set up in Blueprint and data assets, in single player and multiplayer.

The problem

A lock that only turns the camera leaves the fight half done. Attacks still play straight ahead, so a swing at an enemy a step to the side cuts the air, and a lunge at a close enemy runs into it. Switching targets needs its own rules too: which enemy a flick means, and when a lock should let go.
The Targeting component scores every enemy in range and holds the lock. Attack Steering reads the same target, so every attack turns to it, closes the gap and stops short of it.

Try it in the demo

Play LVL_UPC_Demo and step onto the Over Shoulder style plate, whose arena holds three bosses. Press the middle mouse button (top face button on a gamepad) to lock the enemy nearest the screen's center, and move the mouse sideways (the right stick) to switch to the next. Attack: each swing turns to the locked enemy. Press the button again to let go.

Lock-on on your own character

Set it up

5 minutes
  1. 1

    Make the enemies targetable

    A character built on UPC Character Base is targetable already. On any other enemy, click Add and pick UPC Targetable, and set Aim at Socket to its chest bone, such as spine_03 on the engine's mannequin.
  2. 2

    Give the player the lock button

    Create an Ability Set (right-click in the Content Browser, UPC > Actions > Ability Set) and add a row: Ability UPCLockOnAbility, Input Tag UPC.Input.LockOn, Input Action IA_UPC_LockOn. On a character built on UPC Character Base, pick the set as Starting Ability Set in its Class Defaults. A character that keeps its own parent has no such slot: add the set to Ability Sets to Grant on its Default Item's Equippable fragment (Character Setup). The demo's AbilitySet_UPC_Default carries this row already, and the demo controller's IMC_UPC_Default maps IA_UPC_LockOn and the flick action IA_UPC_Flick; on a controller of your own, add a mapping context that maps both.
    AbilitySet_MyCharacter
    ▾Granted Gameplay Abilities > Index [ 0 ]
    AbilityNone
    Input TagNone
    Input Action
    None▾
  3. 3

    Pick a config that locks and switches

    A character's targeting comes from its Combat Style, the Style on its CombatComponent; the demo player starts on CombatStyle_UPC_OverShoulder. Duplicate the style, set the copy as Style, and in the copy's Targeting fragment set Targeting to TargetingConfig_UPC_Soulslike. That config locks within 20 degrees of the screen's center, lets go past 25 m, and switches on the flick action IA_UPC_Flick. Set Steering to Toward Target.
    A character with no style takes the config in its TargetingComponent's Targeting Config instead. One not built on UPC Character Base adds UPC Combat, UPC Targeting and UPC Attack Approach first (Combat Styles).
  4. 4

    Lock on and swing

    Press Play, lock on, and attack from a few steps away and off to one side: the character turns to the locked enemy and closes in. The demo sword's montages carry the windows that steer an attack; a montage of your own needs one, which the next section adds.

Make attacks land on the target

Attack Steering moves the character during the wind-up of an attack, through a window you mark on the montage:
Your attack montageAdd
Animated on the spotA UPC Approach Window across the wind-up, ending where the strike begins. The character turns to the target and glides into range.
Moving by root motionThe engine's Motion Warping notify state across the travel, with Root Motion Modifier UPC Skew Warp and Warp Target Name UPCTarget. The character needs a Motion Warping component.
Offtarget
Plays where it was aimed. The swing travels its own 160 cm and lands beside the target.
Toward Targettarget
Turns to the target and closes in, landing the stop distance short of it.
Distance Onlytargetin the path
Goes where the body faces, as animated, and stops short of the enemy its path runs into. The target off to the side is left alone.
Seen from above, the character starting at the dashed circle and facing up the page. The dashed ring marks the stop distance around the body an attack stops short of; blue is where the attack ends and the arrow the way it faces.
The attack stops the stop distance short of its target, 150 cm unless the style, the weapon or the window sets another, so it lands the blow instead of running into the enemy. Keep Distance also stops the attack short of other enemies in its path. Attack Steering covers both windows and every distance setting.
While a lock is held, the style's Attack Facing turns the body to the locked enemy as each attack starts, whichever of Aim, Movement or Lock Target it is set to; None leaves the body as it was.

Which enemy the lock picks

Without a lock, the Targeting component keeps a soft target: the best-scoring enemy in range, which attacks steer at. A lock press picks again with the config's Lock Score Policies, so a narrow cone around the screen's center can lock the enemy you are looking at over a nearer one off to the side.
view directionA0.73 targetB0.71C0.49Dvetoed: Line Of Sightplayercamera
CandidateDistanceDistance scoreAngle off viewView Angle scoreBlend
A7.2 m0.648°0.910.73
B4.9 m0.7635°0.610.71
C15.0 m0.252°0.980.49
D5.7 m0.7226°0.71vetoed
A selection pass under the demo's Soulslike soft-target policies: Distance (Weight 2), View Angle (Weight 1) and Line Of Sight (Weight 0, a veto only). Each score is the weighted average of the two scoring policies; D sits behind a wall, so Line Of Sight drops it whatever it would score. The highest score is the target.
Setting on the Targeting ConfigDoes
Scan RadiusHow far the scan looks for enemies, in centimeters.
Lock Score PoliciesHow a lock press ranks enemies; a View Angle policy with its veto on and a tight angle makes a lock cone. Empty locks the soft target.
Target TeamsWhich teams may be picked: Enemies Only, the default, leaves teammates out.
Allow LockOff turns lock-on off for this config.

Switch targets with a flick

Set the config's Switch Input Action to an Axis2D action that is active while locked, such as the demo's IA_UPC_Flick, and a flick of the stick or mouse moves the lock to the enemy that way. A stick resting on one side switches once; it has to come back before the next flick counts.
From Blueprint, Switch Lock Target switches in a direction you give, Lock Target locks an actor, Clear Lock lets go, and On Lock State Changed fires when a lock is taken, moved or let go (Targeting).

When the lock lets go

A lock lets go when its enemy dies or stops being targetable, joins a team the config leaves out, or stays farther than Lock Break Distance for Lock Break Grace Seconds (1 second); a Lock Break Distance of 0, the default, turns the distance check off. Untick Targetable on an enemy, or call Set Targetable, as it goes down, and every lock on it lets go.

Keep the camera on the target

While a lock is held, the style's lock camera turns the view: Camera, Lock Follow aims at the target, and Camera, Frame Both keeps the character and the target in frame together (Camera).
To strafe around a locked enemy, set Locked Rotation Mode in the style's Rotation fragment, the body's turning while a lock is held, to Camera Facing or Controller Desired: the body turns with the camera, which the lock keeps on the enemy, so a sideways step circles it. The demo's Soulslike style faces where it moves, turns with the camera while locked (Controller Desired), and turns each attack toward the stick, or to the locked enemy while a lock is held (Attack Facing Movement) (Combat Styles).
AdvancedFlick tuning
SettingDoes
Flick Activation ThresholdHow far, 0 to 1, the stick must be pushed to switch; 0.6 ignores drift.
Flick Rearm ThresholdHow far it must come back before the next flick counts; 0.3.
Switch Cooldown SecondsThe shortest gap between two switches; 0.25.
Flick Cone Half Angle DegreesHow far off the flick an enemy may sit and still be picked; 60.
Flick Direction Weight, Flick Proximity WeightMatching the flick's direction (0.35) against stepping to the enemy nearest the current lock on screen (0.65).
MultiplayerLock-on online
  • Picking and locking run on the player's own machine, so the lock answers the press at once. The player's machine tells the server its target and lock whenever either changes, and an attack carries its own target.
  • The server ignores a reported target beyond Max Reported Target Range (3000 cm), or one the character may not target.
  • The Targeting Config, Steering Mode and Attack Facing do not replicate: set them on every machine, as a style's fragments on Every Machine do.
  • Put the team on the pawn with a UPC Team component, since another player's machine reads a controller's team as none.

Common questions

Does it work with a gamepad? Yes. The demo locks on the top face button and switches with the right stick.
Can I have a soft lock without a lock button? Yes. With no lock held, attacks steer at the soft target, the best-scoring enemy in range. The demo's Freeflow style plays this way, with no lock-on at all.
Can I draw a marker on the locked enemy? Yes. Try Get Current Target Point and On Target Changed feed a HUD marker; tick Continuous Selection on the config so a soft target's marker keeps up as the pick changes (Targeting).
Can it lock onto something that is not a character? Yes. Anything with an enabled UPC Targetable can be picked and locked, as long as the config's Scan Object Types include its collision type; Aim at Socket or Aim at Component Tag picks the point the lock aims at.
Do I need C++? No. Every step on this page is a component, an asset or a setting, and a score policy of your own is a Blueprint class.

PageWhy
TargetingEvery setting on the Targeting component and its config
Attack SteeringApproach windows, warps, stop distances and Keep Distance
Combat StylesOne asset for rotation, targeting, reach, movement and camera
CameraThe lock-on camera and the rest of the camera rig