Skip to content

MuharremTozan/Agentic-Duel / .agent/skills/unity-fsm

unity-fsm

Verified

Specialized skill for implementing a robust, extensible Finite State Machine in Unity using the State and Strategy patterns. Based on the pattern by Adam Myhre (3D Platformer). Use when creating complex AI, player controllers, or any system requiring structured state management.

references/guide.md

View on GitHub
# Finite State Machine Guide (Adam Myhre Pattern)

This guide explains how to implement the Object-Oriented Finite State Machine as used in Adam Myhre's 3D Platformer project.

## 1. Core Architecture
The machine uses the **State** and **Strategy** patterns. All file templates can be found in `assets/code/`.

- **IState**: Core interface for state logic.
- **StateMachine**: manages a dictionary of `StateNode` objects and handles transitions.
- **IPredicate**: Defines the condition strategy for transitions.

## 2. Setting Up States
Create a base state to share common references like the controller and animator.

```csharp
namespace Platformer {
    public abstract class BaseState : IState {
        protected readonly PlayerController player;
        protected readonly Animator animator;

        protected BaseState(PlayerController player, Animator animator) {
            this.player = player;
            this.animator = animator;
        }

        public virtual void OnEnter() { }
        public virtual void Update() { }
        public virtual void FixedUpdate() { }
        public virtual void OnExit() { }
    }
}
```

## 3. Implementing Transitions
Transitions are added in the `Awake` method of your main controller.

```csharp
void SetupStateMachine() {
    stateMachine = new StateMachine();

    // Initialize States
    var locomotionState = new LocomotionState(this, animator);
    var jumpState = new JumpState(this, animator);

    // Define Transitions
    stateMachine.AddTransition(locomotionState, jumpState, new FuncPredicate(() => jumpTimer.IsRunning));
    stateMachine.AddTransition(jumpState, locomotionState, new FuncPredicate(() => grounded && !jumpTimer.IsRunning));

    // Set Initial State
    stateMachine.SetState(locomotionState);
}
```

## 4. Updates
Ensure you call the machine's update methods in Unity's lifecycle:

## 5. Hybrid Usage (ActionState)
For simple states that don't need a full class, use `ActionState` to reduce boilerplate.

```csharp
void SetupSimpleMachine() {
    var stateMachine = new StateMachine();

    // Create a state on the fly with lambdas
    var idleState = new ActionState(
        onEnter: () => animator.Play("Idle"),
        onUpdate: () => Debug.Log("Idle...")
    );

    // Combine with complex class states
    var complexState = new MyComplexCustomState(this, animator);

    stateMachine.SetState(idleState);
}
```
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall