Attempting to implement a directional blocking mechanic.

I have a thing I’m developing, and have been struggling with trying to figure out a certain system I wanted to implement. In essence, it’s a top down game (using the third person template) in Unreal Engine 5.1, and the player rotates toward the mouse. What I’m trying to do is make it so the player can block while holding left click, and if they’re hit by an attack in that direction, it will successfully block it. But if an attack comes from say behind or to the side, it would still hit them.

I’ve set up a bit of code as a base where it will block attacks from any direction, but I want it to only be in the general direction the mouse is facing, and thus the player is rotated toward.

I should note, the enemies are using a line trace to attack the player.

If there’s any questions for clarity on the blueprints I’m using, I’m happy to screenshot them and send them over.


This is the current Blueprint in the enemy for if the Trace Shot hits the player.


And just a simple input at the moment on the player character.

You’re looking for dot product which allows you to compare 2 directional vectors and tells you how aligned they are.

Dot product player’s facing direction against the direction of the incoming attack. You can turn into degrees if that’s easier to work with.

Ahhhh, I see. And hello again!

So, I’m guessing I would use Dot Product XYZ (Vector 4), or is it one of the other Dot Product ones since there’s a few I’m noticing.

Imagine the big cube at the top is the enemy attacking the player:

Dot Product XYZ (Vector 4)

3d vector swill do:

image

Use 3d if you’re expecting to be attacked from above / below, too. If you’re game is flat, you could get away with 2d.


image

Print this to get the feel of what dot is doing.


a thread with relevant examples:

Just one question for clarity, the Less Than node, with the 0.9 value, would bringing it up/down increase how much sort of room for error in the trace would exist? Or how exactly does it affect the dot product?

1 Like

Yup. It’s the threshold. Check the other thread for details, especially this bit:

Alright, thanks again for the help!

1 Like

Heya, so, I set it up in the way you mentioned, but it seems like it always registers as a hit no matter what direction the player is facing.

Where is this script? Because this should be location of the enemy:

If you have the direction of the attack as you mentioned above, you don’t even need to calculate it.

It’s in the actual enemy blueprint.

Do note that this does not take into account the direction the enemy is facing, it only cares about the direction of their attack hitting the player.

Yeah, that it kinda what I was aiming for. So, what part of the enemy should I be tracking for it to properly be facing the player? The Trace Shot? I left the Get Actor Location as self since I kinda assumed from the above layout that you were just using a reference to the cube as itself for it.

Enemy forward dot player forward. This will tell you how well the enemy is facing the player - can they even attack. Then you can do another dot to figure out whether the player can block it.


So, in essence, I should change the Get Actor Location for the enemy to a get forward Vector for their mesh?

I’ll note, all I really need to know for the Blueprint I believe, is if the player is actually facing the direction of the enemy that’s facing them, so I can just turn that into a Boolean for if the damage will go through to the player or not on a branch if they’re guarding toward them.

These are 2 separate checks, the same method. If one of the checks fails, the whole thing fails.

Ok, so if it’s 2 seperate checks to try and do that, is there a way of just making it one check for if the player is facing toward the enemy? Like, a way of having the dot product track just the direction the player is facing, and the location of the enemy to make sure the player is looking at whoever’s attacking them? Because I tried setting it up as you showed, but I can’t get the actor location of anything in the enemy outside of just the self reference.

The attack itself will be a form of communication that will produce either the reference of the attacker / defender, or the data. Line trace will give you direction:

  • direction of the attack:

  • damage or overlaps will produce actor references:

image

  • with Point Damage and Hit Events often producing too much data:

Including pre-calculated Shot from Direction. It’s not about shooting, as the name suggests, it’s just direction.

You surely have a method for the attack - get your data there.


I tried getting the data from it like that, but still no dice. Always hits regardless of what way the player is facing when it traces.

I’m not sure what the above means. Who is doing this? How is the trace done? Is this the enemy hitting the player?

When the enemy gets close to the player, it uses the line trace by channel node shown in the image, and if it hits the player, it casts to them as the Out Hit Hit Actor.

I have the below set as you said to get the line traces direction, and the players forward vector. But again, still always triggering the damage and not blocking no matter what way they face in testing.