Search Results for

    Show / Hide Table of Contents

    Example FSM

    Description of the Example Finite State Machine stored in the Samples folder under the ExampleFSM

    Assets\Samples\ExampleFSM
    

    This ExampleFSM is defined under the script ExampleSMAnim which implements the FixedSMAnim abstract class.

    State Machine Design

    The design goal of this state machine is to provide the basic implementation of a character controller with a few supported requirements.

    1. Animations for player movement.
    2. Moving and jumping actions.
    3. Show off different transition types with yawn animation if player does nothing for some period of time.

    To keep this example simple, I did not add actual movement and camera controls as that code can get complex quickly and I wanted to keep this example as simple as possible to show off how to use the FixedSMAnim

    State Machine Definition

    Given this constraint, the state machine has only two inputs of moveAction and jumpAction

    The moveAction is a Vector2 input and jumpAction is just a button press action.

    The design of the example state machine is as follows:

    States

    • Idle: ExampleSMAnim.IdleState
    • Jump: ExampleSMAnim.JumpState
    • Yawn: ExampleSMAnim.YawnState
    • Walking: ExampleSMAnim.WalkingState
    • Punching: ExampleSMAnim.PunchingState
      • note the punching animation is a "locked" animation so it will continue to play even if the player has exited this state.

    Events

    • ExampleSMAnim.MoveEvent - Event raised whenever the player hits a move input.
    • ExampleSMAnim.IdleEvent - Event raised whenever the player stops hitting the move input.
    • ExampleSMAnim.JumpEvent - Event raised whenever the player hits the jump input.
    • ExampleSMAnim.PunchEvent - Event raised whenever the player hits the punch input.

    Transition Diagram

    stateDiagram-v2
      [*] --> Idle
    
      Idle --> Jump : JumpEvent
      Idle --> Walking : MoveEvent
      Idle --> Yawn : Wait 10 seconds
      Idle --> Punch : PunchEvent
    
      Yawn --> Walking : MoveEvent
      Yawn --> Jump : JumpEvent
      Yawn --> Idle : Animation completed
      Yawn --> Punch : PunchEvent
    
      Walking --> Idle : IdleEvent
      Walking --> Jump : JumpEvent
      Walking --> Punch : PunchEvent
    
      Jump --> Idle : Animation completed
    
      Punch --> Jump : JumpEvent
      Punch --> Idle : IdleEvent
      Punch --> Walking : MoveEvent
      Punch --> Idle : Animation completed
    
    • Improve this Doc
    In This Article
    Back to top State Machine Unity Documentation