Skip to content

A powerful lightweight Finite State Machine for Unity, taking full advantage of editor.

License

Notifications You must be signed in to change notification settings

mra1n/Unity-FSM

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity Finite State Machine (FSM)

A powerful lightweight Finite State Machine for Unity, taking advantage of reflection and the editor.

Unity Download Link License MIT

AboutUsageInstallationSupport

About

This Unity FSM is extremely lightweight but carried by the power and utility of the Unity Editor.

Rather than handling states through ScriptableObjects or MonoBehaviours, all states are light classes that are all collected and edited within the FSM Manager. From the FSM Manager, custom default values can be assigned without requiring any form of recompile or edit to the original script.

Unity Download Link

All public fields that are within the state will be shown and can be edited from the manager. When an instance changes to that state, all set values will be passed over.

Use case examples will be soon added to the repository!

Usage

Because of it's extremely light system, there are no transition behaviour or conditions you see in other state machines. It is purely a state only system. On each compile, new created states will be recognized by the state manager, so they can also be edited.

Scene Setup

Running any FSM is extremely simple - all you require is a manager component so that the states can be retrieved: image

And for instances, just add a State Machine to the required game objects. From here you can select the state the instance will start with, and see runtime information on the current state and the previously ran one. image

State Creation 101

To create a state, simply extend the base state or any custom state made:

public class ExampleState : State
{

}

States will be grouped in the FSM Manager based on their namespace. This decision was chosen as it was the most convenient and structured way to group states based on the structure of the project.

For example, if MoveState was in States.Character.Move, this would appear in the "Move" category in the Manager:

image

Categories will always take the final term in the namespace.

State Methods

The following methods are the standards in each state:

/// <summary>
/// Called when entering the state. Use for getting references or setting up behaviour
/// </summary>
public virtual void OnEnter()
{

}

/// <summary>
/// Called when exiting the state.
/// </summary>
public virtual void OnExit()
{

}

/// <summary>
/// Update tick
/// </summary>
public virtual void Tick(float delta)
{
    age += Time.deltaTime;
}

/// <summary>
/// Fixed update tick
/// </summary>
public virtual void FixedTick(float delta)
{
    fixedAge += Time.fixedDeltaTime;
}

/// <summary>
/// Late update tick
/// </summary>
public virtual void LateTick(float delta)
{

}

/// <summary>
/// Debug update tick
/// </summary>
public virtual void DebugTick(float delta)
{

}

OnEnter - Will be called once when the state is created.

OnExit - Will be called once when the state is exited.

Each update method has a delta parameter that will pass Time.DeltaTime. For FixedUpdate this turns into FixedDeltaTime.

Tick(float delta) - Represents Update

FixedTick(float delta) - Represents FixedUpdate

LateTick(float delta) - Represents LateUpdate

DebugTick(float delta) - An additional update method for OnDrawGizmos. Normally used for debugging.

Switching States

Switching states is done through accessing the FSM MonoBehaviour on the GameObject:

// Inside a State Class...
public override void Tick(float delta)
{
  // If the state is older than or equal to 10 seconds
  if (age >= 10)
  {
    parent.SetState(new OtherState());
  }
}

Switching State Parents

If for any reason states require a new parent, you can do so through the SetParent(StateMachine parent) method.

Installation

Unity PackageZipTar

You can also clone the repository through git using the following: git clone https://github.com/WooshiiDev/Unity-FSM.git

Support

Please submit any queries, bugs or issues, to the Issues page on this repository. All feedback and support is massively appreciated as it not only helps me, but allows the projects I work on to be improved.

Reach out to me or see my other work through:

About

A powerful lightweight Finite State Machine for Unity, taking full advantage of editor.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%