Player Input Direction vs Other Actor Rotation

Hi, I was wondering if anyone know how to check if the player input direction is facing away from another actor or pointing right at it.

The question may be unclear so I’ll try to explain with an example. Let’s say there is a enemy directly behind you, by pressing the forward input, you (the player) will be moving backwards/away from the enemy and pressing backwards is move forward/towards the enemy and vise versa if the enemy is in front, right, left, or diagonal from the player.

A great example that uses this is from the Devil May Cry games when you target an enemy.

Any suggestions would be great! Thanks! :+1:

1 Like

Absolutely, @ArcReed3215! It’d require a lot of math and DOT-product stuff but it’s doable. What exactly is your goal with this? A lock-on system alone can get really complicated, and exponentially so if you’re relating your sides/back to a target look-at vector.

2 Likes

This is not particularly hard.

Let’s assume you have TargetPos and YourPos.
Then calculate Vector::Normalize(TargetPos - YourPos)
This is your Forward. (You may wish to zero out the “Z” component of that, if you have level differences in the game.)

Now, if Forward is X and Up is Z (which it is in Unreal), you construct Right through cross product:
Right = Vector::CrossProduct(Vector::Up, Forward)
(Note that Right here is using the Unreal Engine left-handed coordinate system.)

Someone will likely also point out that you can do much of this by using LookAt, which is a correct observation, and may feel simpler to those who understand the underlying matrix operation of finding the look-at matrix.

In Blueprints, it looks like this:

Note that cross product is order dependent – flipping the two input values makes the output vector go the other way.

To make the movement go in the right direction, use Add Movement Input with these “forward” and “right” vectors scaled by the input control values.

2 Likes

I’m trying to build a combo system based on the players input direction and the targets rotation. I drew out an example so you can visualize it better.

(my drawing skills are not very good, sorry)

So let’s say the blue circle is the player and the red circle is the enemy. Topically ‘W’ key would be forward and ‘S’ key would be backwards. But because the enemy is coming at you diagonally, ‘W’ & ‘D’ keys will be forward and ‘S’ & ‘A’ keys would be backwards. If the enemy was coming at the player directly in front of him, the ‘W’ key would be forward and ‘S’ key would be backwards.

The inputs should be reversed if the enemy is coming at the in the opposite direction. For example, if the enemy is coming from behind the character then ‘S’ would be the forward and ‘W’ would be backwards.

Hope This Makes Sense. :thinking:

1 Like

Now that you mentioned it, I totally forgot about the LookAt node!

I’ll give both your suggestions a try, thanks so much for the reply. :+1:

1 Like

I believe! You’ve got this! You just need to guarantee a way of controlling your character independently of a “specified target”, which really is LESS steps.

Then from there, yes, it’s as jwatte said, and you can use look-at’s output to generate your combo system! With the start being directional input and the end being the enemy’s location, that is :slight_smile:

2 Likes

Hi @jwatte, thanks again for the suggestion. I gave your suggestion a try and it works although I had something slightly different in mind. So one of my combo requires the character to move towards to enemy direction and press the attack input. By using the code you suggested, I would have to wait until the character rotates in that direction before it passes all the conditions to trigger the combo.

I’m looking for a way to check the input direction to see if that input direction would meets the requirements to trigger the combo attack. So I would not have to wait for the player to rotate in that direction; if the input direction WOULD rotate the character in right direction and meets the requirements then it would trigger even if the player is not facing that direction.

This might be a little complex, but any suggestion would be great!
Thanks again! :+1:

There’s two things to un-tangle here.

First: Movement, versus rotation. You don’t need to have “face to movement direction” turned on. And if you do, you could make the configured rotation speed be quite fast. But it’s totally reasonable (in some designs) to strafe rather than turn when moving sideways, and keep turning entirely within player control.

Second: Combo trigger. Most combo systems trigger based on input not based on movement. So, if the combo is “move towards enemy, then press X” then your trigger state machine should see a history of “move towards enemy” (which is presumaby “stick up” or “W key,” which maps to “move forward axis.”) followed by an input of “X button pressed.” How the player pawn actually gets moved by the physics/simulation, is not part of a typical combo system trigger sequence.

1 Like