Hello,
basically you can do this with
this code.
You just need to calculate the angle of your trace and compare it with the angle of the hit entity. If the absolute difference between the angles is greater than, lets say, 90, the entity was hit from behind.
So you would probably do it like that:
if (abs(ang(ang_to_target.pan - hit_entity.pan)) > 90 ) ...