I'm having an issue with finding an angle between two vectors. I have this code (pic below) in blueprints, which uses dot product in order to find cos of the angle between two vectors.
However, it seems that this method does not give me full information - I still cannot determine if the object, which position I am tracking, is left or right from me. I believe it is because cos is even function and does not provide information about angle's sign.
Is there any way to determine angle's signed value? May be some C++ magic? I would really appreciate some help in this case.
Dot product will only give you the cosine of the angle formed by the two vectors. It is positive of they are facing the same direction, negative for opposite directions and 0 if they are perpendicular.
Simply check the "RightDirection" to get the sign that corresponds to the original dot product.
By doing this, you will exclude the zeros given by the cardinal directions, so substitute the opposite axis when zero.
Here is the Blueprint pseudocode:
Vector TargetDirection = Normalize(Player.GetActorLocation - Object.GetActorLocation);
float LR = dot(TargetDirection, Player.GetActorRotation.GetRightVector);
float SignLR = Sign(LR); // returns -1,0,1
float Angle = dot(TargetDirection,Player.GetActorRotation.GetForwardVector);
// catch the dot product zeros at(0,90,180,270...)
// if is a float comparison object with the bool output passed to a branch
if(Angle == 0) Angle = LR; // pick a direction (90 degrees left or right)
if(SignLR == 0) SignLR = Angle; //pick a direction
float Signed Angle = SignLR * Angle;
Note: The variables are not needed, when implementing this code, the output pin is wired directly to the multiple inputs.