Creating transitions
If you remember the transitions created in the Animator Controller, those were basically a collection of conditions that checked whether the state the transition belongs to is active. In our FSM approach, this translates simply as if
sentences that detect conditions inside the states. Let’s create the transitions between our proposed states as follows:
- Add a field of the
Sight
type calledsightSensor
in our FSM script, and drag theAI
GameObject to that field to connect it to theSight
component there. As the FSM component is in the same object asSight
, we can also useGetComponent
instead, but in advanced AIs, you might have different sensors that detect different objects, so I prefer to prepare my script for that scenario. You should pick the approach you like the most. - In the
GoToBase
function, check whether the detected object of theSight
component is notnull
, meaning that something is inside our line of vision. If our AI is going...