- Home /
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!
Answer by saberboy117 · Jul 30, 2011 at 11:27 PM
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
http://unity3d.com/support/documentation/ScriptReference/Vector3.Angle.html