Behavior trees can be seen as a synthesis of a number of other artificial intelligence techniques, such as finite-state machines, planning, and decision trees. In fact, they share some resemblance to FSMs, but instead of states, we think in terms of actions spanned across a tree structure.
Just like decisions trees, we will create three pseudo-abstract classes for handling the process:
Create the base class,
Task
:using UnityEngine; using System.Collections; using System.Collections.Generic; public class Task : MonoBehaviour { public List<Task> children; protected bool result = false; protected bool isFinished = false; }
Implement the finalization function:
public virtual void SetResult(bool r) { result = r; isFinished = true; }
Implement the function for creating behaviors:
public virtual IEnumerator Run() { SetResult(true); yield break; }