ExampleStateMachine

Purpose

Basic Agent state machine that can be modified as needed for simulations.

Features

  • Enter / Exit callbacks
  • Enter from / Exit to callbacks
  • OnUpdate callbacks
  • Fire Events with arbitrary parameters
  • Set permissions on which transitions are allowed for a state and the conditions of the transtions
  • Hierarchical

How to Use

Creating States

AgentStateMachine sm = new AgentStateMachine(agent.agentName, animatorWrapper, agent, agentSystem, worldStateSystem);
 
// Idle
sm.Configure(IdleState)
  .OnEnter(() => sm.animator.Play(IdleAnim01))
  .Permit((int)Trigger.Move, MoveState)
  ;
 
// Move
m.Configure(MoveState)
  .OnEnter(() => sm.animator.SetParameter(MoveParam, Mathf.Clamp01(agent.speed / agent.maxSpeed)))
  .OnUpdate(() => sm.animator.SetParameter(MoveParam, Mathf.Clamp01(agent.speed / agent.maxSpeed)))
  .OnExit(() => sm.animator.SetParameter(MoveParam, 0.0f))
  .Permit((int)Trigger.Idle, IdleState)
  ;
 
// Dead
sm.Configure(DeadState)
  .OnEnter(() => sm.animator.SetParameter(DeadParam, true))
  .OnExit(() => sm.animator.SetParameter(DeadParam, false))
  ;
 
// Crouch
sm.Configure(CrouchState)
  .OnEnter(() => sm.animator.SetParameter(StanceParam, 1))
  .OnExit(() => sm.animator.SetParameter(StanceParam, 0))
  ;
 
// Combat Engage
sm.Configure(CombatEngageState)
  .OnEnter(() => sm.animator.SetParameter(EngagedParam, true))
  .OnExit(() => sm.animator.SetParameter(EngagedParam, false))
  ;
 
// FireTwoHandedGunState
sm.Configure(FireGunState)
  .OnEnter(() => sm.animator.SetTrigger(FireParam))
  .SubstateOf(CombatEngageState)
  ;
 
 
sm.SetDefault(IdleState);
sm.Transition(IdleState); 
PlayerFiniteStateMachine

RidePlayerFiniteStateMachine.drawio

Setting Agent States

Globals.api.agentSystem.SetAgentState(agent, StateMachineUtils.CrouchState); 

Scene Location & Name

Assets/Ride/Examples/StateMachine/StateMachine.unity

Scroll Up