Having learned about fuzzy logic, it may do us well to mix some approaches and probably extend the functionality with finite-state machines. However, fuzzy logic doesn't work directly with values—they have to be defuzzified before they have a meaning within its scope. A Markov chain is a mathematical system that allows us to develop a decision-making system that can be seen as a fuzzy state machine.
This recipe uses the matrix and vector classes that come with Unity to illustrate the theoretical approach and make a working example, but it can be improved with our own matrix and vector classes with the proper implementation of the required member functions, such as vector-matrix multiplication.
using UnityEngine; using System.Collections; public class MarkovTransition : MonoBehaviour { public Matrix4x4 matrix; public MonoBehaviour action; }
IsTriggered
member function:public virtual bool IsTriggered() { // implementation details here return false; }
using UnityEngine; using System.Collections; using System.Collections.Generic; public class MarkovStateMachine : MonoBehaviour { public Vector4 state; public Matrix4x4 defaultMatrix; public float timeReset; public float timeCurrent; public List<MarkovTransition> transitions; private MonoBehaviour action; }
Start
function for initialization:void Start() { timeCurrent = timeReset; }
Update
function:void Update() { if (action != null) action.enabled = false; MarkovTransition triggeredTransition; triggeredTransition = null; // next steps }
foreach (MarkovTransition mt in transitions) { if (mt.IsTriggered()) { triggeredTransition = mt; break; } }
if (triggeredTransition != null) { timeCurrent = timeReset; Matrix4x4 matrix = triggeredTransition.matrix; state = matrix * state; action = triggeredTransition.action; }
else { timeCurrent -= Time.deltaTime; if (timeCurrent <= 0f) { state = defaultMatrix * state; timeCurrent = timeReset; action = null; } }
We define a game state based on the numerical value of the vector 4 member variable, with each position corresponding to a single state. The values in the game state change according to the matrix attached to each transition. When transitions are triggered, the game state changes, but we also have a countdown timer to handle a default transition and change the game accordingly. This is useful when we need to reset the game state after a period of time or just apply a regular transformation.