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/enemy-ai.md

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

This guide covers implementing an AI using the 100% repo-accurate FSM and Detection strategies.

## 1. Core AI Components
- **NavMeshAgent**: Required for AI navigation.
- **PlayerDetector**: Handles detection using `IDetectionStrategy`.
- **Timer (CountdownTimer)**: Critical for handling cooldowns (Detections, Attacks).

## 2. Using the At() Helper
Adam Myhre uses a clean `At()` helper method in the `Enemy` component to wire transitions:

```csharp
void Start() {
    stateMachine = new StateMachine();
    
    // 1. Initialize States
    var wanderState = new EnemyWanderState(this, animator, agent, 10f);
    var chaseState = new EnemyChaseState(this, animator, agent, detector.Player);
    
    // 2. Define Transitions with Helper
    At(wanderState, chaseState, new FuncPredicate(() => detector.CanDetectPlayer()));
    At(chaseState, wanderState, new FuncPredicate(() => !detector.CanDetectPlayer()));

    // 3. Set Initial State
    stateMachine.SetState(wanderState);
}

void At(IState from, IState to, IPredicate condition) => stateMachine.AddTransition(from, to, condition);
```

## 3. The Timer System
The FSM is tightly coupled with `Utilities.Timer`. Always use `Tick(Time.deltaTime)` in your `Update` loops to keep your AI logic heartbeat going.

```csharp
void Update() {
    stateMachine.Update();
    attackTimer.Tick(Time.deltaTime); // Don't forget this!
}
```

## 4. Detection Strategy (Strategy Pattern)
The `PlayerDetector` uses an `IDetectionStrategy`. You can swap `ConeDetectionStrategy` for any other custom implementation.

```csharp
// Example check in a transition predicate
() => detector.CanDetectPlayer() 
```
This returns true if the player is within the cone OR if the `detectionTimer` is still running from a recent detection.
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