Created
February 19, 2021 10:38
-
-
Save johans2/3fc8c0e581d4195d96bdd79dcc5f8c32 to your computer and use it in GitHub Desktop.
Hierarchical statemachine setup example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using HFSM; | |
public class MoveState : StateMachine { | |
protected override void OnUpdate() { | |
Debug.Log("Updating MoveState"); | |
} | |
} | |
public class RunState : StateMachine { | |
protected override void OnEnter() { | |
Debug.Log("Enter RunState"); | |
} | |
protected override void OnUpdate() { | |
Debug.Log("Updating RunState"); | |
} | |
protected override void OnExit() { | |
Debug.Log("Exit JumpState"); | |
} | |
} | |
public class JumpState : StateMachine { | |
protected override void OnEnter() { | |
Debug.Log("Enter JumpState"); | |
} | |
protected override void OnUpdate() { | |
Debug.Log("Updating JumpState"); | |
} | |
protected override void OnExit() { | |
Debug.Log("Exit JumpState"); | |
} | |
} | |
public class Triggers { | |
public const int JUMP = 1; | |
public const int RUN = 2; | |
} | |
public class Example : MonoBehaviour | |
{ | |
StateMachine moveState = new MoveState(); | |
StateMachine runState = new RunState(); | |
StateMachine jumpState = new JumpState(); | |
void Start() | |
{ | |
moveState.LoadSubState(runState); | |
moveState.LoadSubState(jumpState); | |
moveState.AddTransition(runState, jumpState, Triggers.JUMP); | |
moveState.AddTransition(jumpState, runState, Triggers.RUN); | |
moveState.EnterStateMachine(); | |
} | |
void Update() | |
{ | |
moveState.UpdateStateMachine(); | |
if (Input.GetKeyDown(KeyCode.J)) { | |
moveState.SendTrigger(Triggers.JUMP); | |
} | |
if (Input.GetKeyDown(KeyCode.R)) { | |
moveState.SendTrigger(Triggers.RUN); | |
} | |
} | |
void OnDestroy() { | |
moveState.ExitStateMachine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment