I’ve been spending way to much time on a seemingly simple problem: I’m working on an third-person game and need to know where an NPC is in relation to the player. So for example I want a certain action only to happen while the NPC is on the left-hand side of the player (lets say between -45 and -135 degrees in relation to the players current rotation).
I’m aware of “Find Look At Rotation” but this gives me only a global rotation (?) and doesn’t change when I turn my character. (This calculation has to be only in 2D, I don’t care whether the NPC is higher or lower.)
Hi, you can use DotProduct Dot Product | Unreal Engine Documentation between the normalized forward vector of the player (GetActorForwardVector) and the normalized vector from the player to the NPC (Normalize(LocationNPC - LocationPlayer)).
This here will give you the angle between the forward vector of the player and the vector from the player to the NPC
Also the dot product will return 1, if the angle between both vectors is 0 degree, -1 if the angle is 180 degree, 0 if the angle is 90 degree and something in between if the angle is something in between. If you want the exact angle, then you can use arccos of the dot product which will give you the exact angle between both vectors.
Thank you, this seems to be a large step in the right direction for me. The only problem is that I can’t distinguish left from right. Whether the NPC is on players left or right gives me exactly the same numbers. Is there a way to get this signed or any other methode to check for left or right?
With some more searching I think I figured it out. You can determine “left” or “right” by calculating the cross-products of both vectors ad checking it’s direction. So the following works for me: