I’m creating a third person melee game and I’m trying to replicate the way that God of War automatically snaps the player character to enemies if they’re within a certain range and radius of them. Jason DeHeras has a quick video of this feature in this tweet
I’ve seen another post about this and the only response was:
“On Melee Attack Overlap Sphere to get Potential targets in desired Range (Sphere Radius)
Filter out Potential targets that are not in certain Angle in front of you or where your Input Direction goes to or any other filtering you want to do (Math)
Get the Closest one (Math)
Interpolate towards Target (you might want to have a little offset from him too) or do a simple forward attack if no closeby Target was found.”
It sounds like that would work but since I’m relatively new to UE4, I don’t actually know how to go about doing this, specifically the “Filter out Potential targets that are not in certain Angle in front of you or where your Input Direction goes” part.
The Dot Product of 2 directional vectors will be in range of -1 to 1. Anything that’s above 0 is in front of you, anything below 0 is behind (and every other value in between matters, too!)
I grab all the actors around the player using a Sphere Overlap (as in the original suggestion) and loop through the elements of the array, checking my angles - Dot player’s forward and the direction to each target. For dot values greater than .5 I mark the target, and add that actor to the array - these are the baddies in my targeting cone: