Deteecting if an ai can see based on angle

Is it possible to detect if an ai box can see another ai box by using angles? For instance Box A can only see the box B if its between a 45 degree arc in front of it. I know I could detect this sort of thing using raycasts but the way my collision avoidance works it has caused me problems so I’m looking for another way so the ai doesn’t just shoot no matter what direction its facing relative to the target. Any ideas? Thanks in advance!

you can determin the angle between one objects z axis (blue arrow) aka forward direction and anothers position with Vector3.Angle()

to use get a targetdirection (TargDir) using this

TargDir = (target.position - yourposition.position)

then use it like this

var angle : float = Vector3.Angle(TargDir, yourposition.forward)

that will return an angle between your forward and whatever you want to see if its looking at

the offical doc