Follow these steps to integrate the Advanced Targeting System into a new Unreal Engine project. Note that the system is pre-configured in the default project included with Advanced ARPG Combat, so these steps are only necessary for custom setups.
1. Prerequisites:
- Unreal Engine 5.3 or later.
- A pawn (e.g., player character) to host the targeting component.
- Targetable actors (e.g., enemies or NPCs) in the project.
2. Add the Targeting Component:
- Open the Blueprint of the pawn that will perform targeting (e.g., the player character).
- In the Components panel, click Add Component and select `BP_TargetingComponent`.
- Select the `BP_TargetingComponent` in the Details panel and configure properties like `Targeting Distance`, `Rotation Interp Speed`, or `Targeting Object Types` as needed.
3. Set Up Input Bindings:
- In Project Settings > Input, create the following action mappings:
- `ToggleLockOn` (e.g., bound to a key like “T”).
- `SwitchTargetRight` (e.g., bound to a key like “E”).
- `SwitchTargetLeft` (e.g., bound to a key like “Q”).
- In the pawn’s Blueprint, bind these inputs to call the corresponding functions on `BP_TargetingComponent`:
- `ToggleLockOn` for toggling lock-on.
- `Switch To Right` - SwitchToRight` for switching to the rightmost target.
- `Switch To Left` for switching to the leftmost target.
4. Modify Look Input:
- In the pawn’s input handling (e.g., in the Player Controller or pawn Blueprint), add a condition to disable look input (e.g., yaw/pitch) when `BP_TargetingComponent` is in a targeting state. Use the component’s `IsTargeting` property to check this.
5. Set Up Targetable Actors:
- Open the Blueprint of each actor that should be targetable (e.g., enemy or NPC).
- In the Class Defaults, add the `BP_TargetingInterface` to the Interfaces list.
- Add a Widget Component to the actor:
- Set the Widget Class to `WBP_LockOn` (or a custom widget if desired).
- Set the Widget Component’s default visibility to Hidden.
- Implement the `On Target Selected` interface function:
- Use the provided `bTargeted` bool to set the Widget Component’s visibility (e.g., Visible if `bTargeted` is true, Hidden otherwise).
- Add any additional logic (e.g., playing a sound or animation when targeted).
- Implement the `Get Targeting Location` interface function:
- Return the world location of the Widget Component (e.g., using `GetComponentLocation`).
- Implement the `Can Be Targeted` interface function:
- Return `true` if the actor is targetable (e.g., check if the actor is alive or meets other conditions).
6. Test the System:
- Place targetable actors in the level.
- Playtest to ensure the targeting system locks onto targets, switches between them, and displays the lock-on widget correctly.
Troubleshooting:
- If the lock-on widget doesn’t appear, verify that the Widget Component’s Widget Class is set to `WBP_LockOn` and that `On Target Selected` sets its visibility correctly.
- If targeting fails, check that `Targeting Object Types` in `BP_TargetingComponent` includes the relevant object types (e.g., Pawn) and that targetable actors implement `BP_TargetingInterface`.
- If rotation feels jittery, adjust the `Rotation Interp Speed` property in `BP_TargetingComponent`.