{"id":28104980,"url":"https://github.com/madeyellow/finitestatemachine","last_synced_at":"2026-04-20T03:03:20.269Z","repository":{"id":224930069,"uuid":"764618332","full_name":"madeyellow/FiniteStateMachine","owner":"madeyellow","description":"A finite state machine for Unity","archived":false,"fork":false,"pushed_at":"2024-10-27T05:05:29.000Z","size":41,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-28T06:51:36.499Z","etag":null,"topics":["csharp","dotnet","finite-state-machine","fsm","unity"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/madeyellow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-28T12:08:40.000Z","updated_at":"2024-10-27T05:05:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"a689730c-c86b-448b-a5e6-470cf7c05c3c","html_url":"https://github.com/madeyellow/FiniteStateMachine","commit_stats":null,"previous_names":["madeyellow/finitestatemachine"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madeyellow%2FFiniteStateMachine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madeyellow%2FFiniteStateMachine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madeyellow%2FFiniteStateMachine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madeyellow%2FFiniteStateMachine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/madeyellow","download_url":"https://codeload.github.com/madeyellow/FiniteStateMachine/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254029025,"owners_count":22002285,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","finite-state-machine","fsm","unity"],"created_at":"2025-05-13T21:19:00.705Z","updated_at":"2026-04-20T03:03:15.227Z","avatar_url":"https://github.com/madeyellow.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# What is it?\n\nThis package is a foundation that allows you to easily build an FSM in your Unity project using our building blocks. It features:\n\n* FSM execution and state transition logic;\n* Overrideable state enter/exit hook methods;\n* State enter/exit UnityEvents;\n* State execution duration; \n\n## How to install Finite State Machine in my Unity project?\n\nUse the Unity Package Manager (in Unity’s top menu: **Window \u003e Package Manager**), click the \"+\" icon, select **\"Add package from git URL\"** and type the URL of this repository:\n\n```\nhttps://github.com/madeyellow/FiniteStateMachine.git\n```\n\n![Installing package via PackageManager](https://unitology.ru/wp-content/webp-express/webp-images/uploads/image-5.png.webp)\n\n## Getting started\n\nCreating an FSM with use of this package is as simple as defining your state base, the states you need, and FSM itself. Like this:\n\n```csharp\nusing MadeYellow.FSM;\nusing UnityEngine;\n \n/// \u003csummary\u003e\n/// Base for any state in our FSM, refrencing FSM\n/// \u003c/summary\u003e\npublic abstract class MyFiniteStateMachineStateBase : StateBase\n{\n    protected readonly MyFiniteStateMachine FiniteStateMachine;\n \n    public MyFiniteStateMachineStateBase(MyFiniteStateMachine finiteStateMachine)\n    {\n        FiniteStateMachine = finiteStateMachine;\n    }\n}\n \n/// \u003csummary\u003e\n/// Example of some state\n/// \u003c/summary\u003e\npublic class MyState : MyFiniteStateMachineStateBase\n{\n    private float _time;\n \n    public MyState(MyFiniteStateMachine finiteStateMachine) : base(finiteStateMachine)\n    {\n    }\n \n    protected override void ExecuteHandler(in float deltaTime)\n    {\n        // This is logic of your state. You may implement anything here.\n        _time += deltaTime;\n \n        Debug.Log($\"{_time} has passed\");\n    }\n}\n\n/// \u003csummary\u003e\n/// Your FSM\n/// \u003c/summary\u003e\npublic class MyFiniteStateMachine : FiniteStateMachineBase\u003cMyFiniteStateMachineStateBase\u003e\n{\n    public readonly MyState SomeState;\n    public readonly MyState SomeOtherState;\n\n    public MyFiniteStateMachine()\n    {\n        SomeState = new MyState(this);\n        SomeOtherState = new MyState(this);\n\n        ChangeState(SomeState);\n    }\n}\n```\n\nStates define execution logic (e.g. how a character should move, fall, swim, etc.), and FSM gives you the ability to translate between those states. You may use your FSM in some MonoBehaviour like this:\n\n```csharp\npublic class BasicCharacter : MonoBehaviour\n{\n    private MyFiniteStateMachine _fsm;\n\n    private void Awake()\n    {\n        _fsm = new MyFiniteStateMachine();\n    }\n\n    private void FixedUpdate()\n    {\n        _fsm.Execute(Time.fixedDeltaTime);\n    }\n}\n```\n\n### Translating from one state to another\n\nIf you want to translate from state **SomeState** to state **SomeOtherState** you should override the *CheckTransitions()* method in the **MyState** class and use the *FiniteStateMachine.ChangeState()* method like this:\n\n```csharp\npublic override void CheckTransitions()\n{\n    // Some condition to transition from this state. CheckTransitions() of CurrentState executes before ExecuteHandler() each time you call Execute() in your FSM\n    if (_time \u003e 1)\n    {\n        // A way to change state of FSM. FSM will use ExecuteHandler of SomeOtherState instead of this state\n        FiniteStateMachine.ChangeState(FiniteStateMachine.SomeOtherState);\n    }\n}\n```\n\n### Current \u0026 previous states of FSM\n\nIf you want to know which state your FSM is now or was in before you may use the *CurrentState* and the *PreviousState* properties of your FSM:\n\n```csharp\npublic class BasicCharacter : MonoBehaviour\n{\n    private MyFiniteStateMachine _fsm;\n\n    private void SomeMethod()\n    {\n        var currentState = _fsm.CurrentState;\n        var previousState = _fsm.PreviousState;\n    }\n}\n```\n\nYou may even use it inside your state to define various logic based on the previous state:\n\n```csharp\npublic class MyState : MyFiniteStateMachineStateBase\n{\n    private float _time;\n \n    public MyState(MyFiniteStateMachine finiteStateMachine) : base(finiteStateMachine)\n    {\n    }\n \n    protected override void ExecuteHandler(in float deltaTime)\n    {\n        // Execute only if previous state was SomeState\n        if (FiniteStateMachine.PreviousState == FiniteStateMachine.SomeState)\n        {\n                _time += deltaTime;\n        }\n\n        Debug.Log($\"{_time} has passed\");\n    }\n}\n```\n\n### FSM state change event\n\nEach time your FSM changes its state it invokes an *OnCurrentStateChanged* UnityEvent. This may be useful if you want to perform some action on a change of state for some reason.\n\n```csharp\npublic class BasicCharacter : MonoBehaviour\n{\n    private MyFiniteStateMachine _fsm;\n\n    private void Awake()\n    {\n        _fsm = new MyFiniteStateMachine();\n        _fsm.OnCurrentStateChanged.AddListener(OnStateChanged); // Subscribes to change of state in _fsm\n    }\n\n    private void OnStateChanged()\n    {\n        // Some logic\n    }\n}\n```\n\n### Enter/Exit state hook methods\n\nYou can add custom logic to your state when FSM enters or exits certain states. This can be useful to fetch \u0026 cache some data from FSM (like fetching velocity of character to determine how much damage you should apply on fall damage, etc.) or reset something inside your state before first *ExecuteHandler()* will be invoked.\n\nThere are two methods inside your state for it:\n\n```csharp\nprotected override void StateEnteringHook()\n{\n    Debug.Log($\"MyState entereted\"); // This will be called when FSM enter your state\n}\n \nprotected override void StateExitingHook()\n{\n    Debug.Log($\"MyState exited\"); // This will be called when FSM exit your state\n}\n```\n\n### State enter/exit events\n\nIf you want other components to execute some logic when your FSM enters or exists in some specific state (e.g. show a character's fall animation in the animator when entering \"air\" state, or showing attack animation when entering \"melee attack\" state, etc.) you may use the state's *OnEnteredState* and *OnExitedState* UnityEvents.\n\n```csharp\npublic class BasicCharacterAnimator : MonoBehaviour\n{\n    private MyFiniteStateMachine _fsm;\n\n    private void Awake()\n    {\n        _fsm.SomeState.OnEnteredState.AddListener(OnSomeStateEntered);\n    }\n\n    private void OnSomeStateEntered() {}\n}\n```\n\n### State execution duration \n\nIf, for some reason, you'll need to know how long a certain state has been executing since last entering it, you may use the state's *ExecutionDuration* property, which tells how many in-game seconds this state is actually executing.\n\n```csharp\npublic class MyState : MyFiniteStateMachineStateBase\n{\n    public MyState(MyFiniteStateMachine finiteStateMachine) : base(finiteStateMachine)\n    {\n    }\n \n    protected override void ExecuteHandler(in float deltaTime)\n    {\n        Debug.Log($\"State is executing for {ExecutionDuration} seconds\");\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadeyellow%2Ffinitestatemachine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmadeyellow%2Ffinitestatemachine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadeyellow%2Ffinitestatemachine/lists"}