For the angle detection you utilize the power of the cosine (or sine, depending which is optimal).

Get the angle the damaged unit is facing. Let us call this angleface.

Get the angle the damage source is from the perspective of the damaged unit. Let us call this anglesource.

Work out the angle which where damage should be blocked from. This is done by getting angleface and adding 90 degrees to it. Let us call this angleblock. angleblock = (angleface + 90)

Work out the angle difference from angleblock to anglesource. This is done by getting anglesource and subtracting angleblock. Let us call this anglediff. anglediff = (anglesource - angleblock)

Utilize cosine to convert anglediff into a comparable facing deviant. Simply put anglediff through cosine. Let us call the result anglecomp. anglecomp = (Cos(anglediff))

Compute a threshold value for the angle based on the maximum deviation you want to permit from the block angle. This is done by putting the amount through the cosine function as well. In this case the threshold value is 45 degrees. Let us call this compthresh. compthresh = (Cos(45))

Finally you can test if a hit should be blocked or not by checking if anglecomp is above the threshold. In this case it would be (anglecomp greater than or equal to compthresh).

If you inline the mathematics you get...

(Cos(anglesource - (angleface + 90)) >= Cos(45))

(Cos(anglesource - angleface - 90) >= Cos(45))

As we know from mathematics, the Cosine and Sine functions are related to each other by an input offset of 90 degrees. As such we can optimize this by replacing the cosine with a sine function.

(Sin(anglesource - angleface) >= Cos(45))

Due to the circular nature of Sin and Cos functions any intermediate angles <0 or >360 does not matter.