Working a finite-state machine
Another interesting yet easy-to-implement technique is finite-state machines (FSM). They move us to change the train of thought from what it was in the previous recipe. FSMs are great when our train of thought is more event-oriented, and we think in terms of holding behavior until a condition is met changing to another.
Getting ready
This is a technique mostly based on automata behavior, and will lay the grounds for the next recipe, which is an improved version of the current one.
How to do it...
This recipe breaks down into implementing three classes from the ground up, and everything will make sense by the final step:
- Implement the
Condition
class:public class Condition { public virtual bool Test() { return false; } }
- Define the
Transition
class:public class Transition { public Condition condition; public State target; }
- Define the
State
class:using UnityEngine; using System.Collections.Generic; public class State : MonoBehaviour...