Download

How to create God of War targetting system?

Hi everyone

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.

Any help would be greatly appreciated.

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:

Even though the one on the right is within range of the sphere, it’s not a valid target as it’s not in range of the requested >.5 dot product value.

To visualise it better, here are the dot values for this setup:

You could also get lazy-clever about it and use an actual cone-shaped mesh instead of a sphere - this way you get *filtered *results only.

Thankyou so much!! I’ll try that out a little bit later tonight then and get back to you but that looks amazing. I really appreciate that!