How to do it...

We will create a state that is capable of holding internal states, in order to develop multilevel hierarchical state machines:

  1. First, let's create the StateHighLevel class deriving from State, as shown in the following code:
using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
 
public class StateHighLevel : State 
{ 
} 
  1. Next, add the new member variables to control the internal states, as demonstrated in the following code:
public List<State> states; 
public State stateInitial; 
protected State stateCurrent; 
  1. Then, we override the initialization function, as shown in the following code:
public override void OnEnable() { if (stateCurrent == null) stateCurrent = stateInitial; stateCurrent.enabled = ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.