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
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 asspine_03on the engine's mannequin. - 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: AbilityUPCLockOnAbility, Input TagUPC.Input.LockOn, Input ActionIA_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'sAbilitySet_UPC_Defaultcarries this row already, and the demo controller'sIMC_UPC_DefaultmapsIA_UPC_LockOnand the flick actionIA_UPC_Flick; on a controller of your own, add a mapping context that maps both.AbilitySet_MyCharacter▾Granted Gameplay Abilities > Index [ 0 ]AbilityNoneInput TagNoneInput ActionNone▾ - 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 onCombatStyle_UPC_OverShoulder. Duplicate the style, set the copy as Style, and in the copy's Targeting fragment set Targeting toTargetingConfig_UPC_Soulslike. That config locks within 20 degrees of the screen's center, lets go past 25 m, and switches on the flick actionIA_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
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 montage | Add |
|---|---|
| Animated on the spot | A 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 motion | The 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. |
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.
| Candidate | Distance | Distance score | Angle off view | View Angle score | Blend |
|---|---|---|---|---|---|
| A | 7.2 m | 0.64 | 8° | 0.91 | 0.73 |
| B | 4.9 m | 0.76 | 35° | 0.61 | 0.71 |
| C | 15.0 m | 0.25 | 2° | 0.98 | 0.49 |
| D | 5.7 m | 0.72 | 26° | 0.71 | vetoed |
| Setting on the Targeting Config | Does |
|---|---|
| Scan Radius | How far the scan looks for enemies, in centimeters. |
| Lock Score Policies | How 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 Teams | Which teams may be picked: Enemies Only, the default, leaves teammates out. |
| Allow Lock | Off 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
| Setting | Does |
|---|---|
| Flick Activation Threshold | How far, 0 to 1, the stick must be pushed to switch; 0.6 ignores drift. |
| Flick Rearm Threshold | How far it must come back before the next flick counts; 0.3. |
| Switch Cooldown Seconds | The shortest gap between two switches; 0.25. |
| Flick Cone Half Angle Degrees | How far off the flick an enemy may sit and still be picked; 60. |
| Flick Direction Weight, Flick Proximity Weight | Matching 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.
Related
| Page | Why |
|---|---|
| Targeting | Every setting on the Targeting component and its config |
| Attack Steering | Approach windows, warps, stop distances and Keep Distance |
| Combat Styles | One asset for rotation, targeting, reach, movement and camera |
| Camera | The lock-on camera and the rest of the camera rig |