No matter how clever our AI is, it needs some senses to become aware of its surroundings. In this recipe, we'll accomplish an AI that can look in a configurable arc in front of it, as shown in the following screenshot. It will build upon the AI control from the previous recipe, but the implementation should work well for many other patterns as well. The following screenshot shows Jaime with a visible representation of his line of sight:
To get our AI to sense something, we need to modify the AIControl
class from the previous recipe by performing the following steps:
We need to define some values, a float called
sightRange
, for how far the AI can see, and an angle representing the field of view (to one side) in radians.With this done, we create a
sense()
method. Inside we define a Quaternion calledaimDirection
that will be the ray direction relative to the AI'sviewDirection
field.We convert the angle to a Quaternion and multiply it with
viewDirection
to get...