Hello everyone. Potentially basic math question from someone who’s not really good at math at all.
I need to calculate the angle between my Player camera’s Forward Vector and a point in the world (for this test, 0,0,0). I’m at this point right now thanks to some google searching and my knowledge of math:
Edit: To clear up a couple things, this is for a 2D radar UI I have. The “objective arrow” is my UI element that rotates based on the rotation angle. Also, because this is a 2D radar, I’m flattening everything to 0 height, that’s why I’m making vectors with the Z set to 0 (I could also compare the Z values to add an upper or lower value later on to tell the player whether the objective is higher or lower than the player, but don’t need that for now).
Use FindLookAtRotation, plug your camera world location and your target world location then subtract your camera world Z-rot to the FindLookAtRotation result Z-rot.
Example:
Camera location (0,0,0) target location (0,1,0). FindLookAtRotation will output 90 deg Z because the target is directly on your right side. If your camera is looking at Rot(0,0,-90) then it is looking on opposite side which is 90-(-90) = 180 deg.
Using Cross Product Z Axis will tell you if the angle is after or before (Clock or anti-clockwise)
The figure shows the angle between forward vector of player and the vector of camera.
Answer to the original question on getting -180 / +180 deg angle from 2 vectors for anyone still looking. This is a function you can add then just call when needed. Only tested where the angle is in a single plane with the angle around the z axis. May need a tweak to the cross product axis you check >0 if the angle is in a different axis, don’t know.
Thank you so much, after looking for days your solution was exactly what i was looking for! It accounts for direction - and + so it was very useful in for a turn in place without snapping.