How to find if an actor is in front or behind another actor?

How do you find whether Actor B is behind Actor A, based on their location/rotations? In my custom FPS setup sometimes targets get closer to the player than the gun barrel. As my projectiles spawn with a Look At Rotation I find my gun firing backwards. I need to check whether the actors are in front (180 degrees for preference, but a configurable angle would be handy) of the gun barrel. Basically a cone of vision check but using maths not line traces.

Thanks in advance!

1 Like

Subtract actor B’s location by actor A’s location and normalize the resulting vector. Then take the normalized vector and get the dot product of it and actor A’s forward vector. Dot product will return a -1 to 1 range depending on the angle. Using the trig function cos will give you the value that will be returned at a given angle. For example, if the value returned from your dot product is greater than 0.86602, then your two vectors are less than 30 degrees apart (cos (30) = 0.86602).


Perfect, thanks for the explanation ausernottaken!

1 Like