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:
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; }
public virtual void SetResult(bool r) { result = r; isFinished = true; }
public virtual IEnumerator Run() { SetResult(true); yield break; }
public virtual IEnumerator RunTask() { yield return StartCoroutine(Run()); }
ConditionBT
class:using UnityEngine; using System.Collections; public class ConditionBT : Task { public override IEnumerator Run() { isFinished = false; bool r = false; // implement your behaviour here // define result (r) whether true or false //--------- SetResult(r); yield break; } }
using UnityEngine; using System.Collections; public class ActionBT : Task { public override IEnumerator Run() { isFinished = false; // implement your behaviour here //--------- return base.Run(); } }
Selector
class:using UnityEngine; using System.Collections; public class Selector : Task { public override void SetResult(bool r) { if (r == true) isFinished = true; } public override IEnumerator RunTask() { foreach (Task t in children) yield return StartCoroutine(t.RunTask()); } }
Sequence
class:using UnityEngine; using System.Collections; public class Sequence : Task { public override void SetResult(bool r) { if (r == true) isFinished = true; } public override IEnumerator RunTask() { foreach (Task t in children) yield return StartCoroutine(t.RunTask()); } }
Behavior trees work in a similar fashion to decision trees. However, the leaf nodes are called tasks and there are some branch nodes that are not conditions, but run a set of tasks in one of two ways; Selector and Sequence. Selectors run a set of tasks and return true when one of their tasks return true, it can be seen as an OR node. Sequences run a set of tasks and return true when all of their tasks return true, it can be seen as an AND node.
3.14.142.194