Sorry for such a long post, but there is a lot to unpack and I tend to be confusing.
TLDR; I am currently working on a third person lock on system similar to Dark Souls, Trials of Mana, For Honor etc. Camera doesn’t unlock when the Player moves away from the Target.
Pictures of the blueprint are in their respective descriptions and video explanation is at the bottom.
When the Player moves away from the Target the Lock On System properly unlocks the camera for a brief moment and disables targeting. Then, after a few seconds, the camera becomes partially locked to the same Target. I mean partially because yaw input is active, but pitch isn’t.
The blueprint starts with an input action that flip flops two custom events; Find Target and Disable Targeting. Input Action - Find Target and Disable Targeting Functions - Imgur
Find Target Custom Event:
First, the system clears all the available targets in the array (which is set later). Then it does a 10 meter sphere trace around the player for any World Dynamic or Pawn classes. Find Target Custom Event - Imgur
Next, is a sphere trace check of all the World Dynamic or Pawns if they have the Enemy Interface. If it does, then it casts a line trace from the player to those objects. If something breaks the line trace then it does nothing. If something doesn’t break the line trace, then it adds those objects to the All Available Targets array to be used later. Enemy Interface Check - Imgur I should note the “Check Actor Collision” node is the collapsed vector math and line trace components.
I am not sure if this next check is 100% necessary… After adding all of the possible targets to the array, the system double checks to see if the array list is at least 1. If it is, then it continues with the targeting. If it isn’t, then it disables targeting. More on this in a moment. Imgur: The magic of the Internet
The system chooses the nearest Target. This is done by getting the distances of all the targets in the All Available Targets array then compares that to the Player’s location. Once this is finished finding the nearest target, the system sets the Nearest Target variable. Choose Nearest Target Function - Imgur
Then, the IsTargeting and Rotation To Target booleans are set to true and the third person character rotates to the Nearest Target with the Smooth Rotation To Target custom event. Imgur: The magic of the Internet Smooth Rotation custom event Imgur: The magic of the Internet
When the Rotation to Target is set to true, it triggers an OnTick event that rotates the Third Person Character around the Target. On Tick Rotation - Imgur Target Rotation float is the location math.
Camera Lock Toggle
After all that is handled, we finally get to the issue. The system sends the IsTargeting boolean to the Enemy with a Blueprint Interface message, which turns on and off the targeting indicator. The timeline is how often the Break Sightline and Distance Fall off custom events occur (every 0.2 seconds). This is easier to use than OnTick in my opinion as it’s right with the rest of the code. Timeline, Sightline, Distance lock off, and BPI Message - Imgur
Camera Look At Function
Vector and rotator math for the camera to follow the target. Camera Look At Target Function - Imgur While this is active, the normal camera input is deactivated. Camera Input Imgur: The magic of the Internet
This little bit sends a line trace to only the Nearest Target. If something breaks the line trace (i.e. a wall), then the targeting is disabled with the Disable Targeting Custom event. More on that later. Imgur: The magic of the Internet This is working as intended.
Distance Lock Off
This is the part that isn’t working as intended. When the player moves out of the targeting radius, the targeting and the camera rotation should be disabled. However, for reasons I can’t figure out, the Timeline (from the camera lock toggle picture Timeline, Sightline, Distance lock off, and BPI Message - Imgur) still activates the Break Sightline and Distance Lock Off Custom Events which activates the Camera Look At Function.
When the conditions are met (moving away from the target, the target is killed, the input action is activated, etc.) then targeting should be disabled. Disable Targeting - Imgur In case of bug targeting is canceled, but the camera rotation isn’t.
I am happy to clarify if I am spouting nonsense or if my Blueprint is weird/has unnecessary parts. Thank you in advance for taking a look.
Credit to ZzGERTzZ on the Unreal Engine Marketplace and Rayziyun for the basis of my blueprint.