RetreatState - jimdroberts/FishMMO GitHub Wiki

Description

AI State for retreating from a target. Causes the NPC to move away from its target until a safe distance is reached.


API Access

Fields

  • public float RetreatDistance

    The distance the NPC will attempt to move away from its target when retreating.

  • public float SafeDistance

    The minimum distance from the target that is considered safe. Once reached, the NPC will stop retreating.

Methods

  • public override void Enter(AIController controller)

    Called when the state is entered. Sets the retreat destination away from the target. Parameters: - AIController controller: The AI controller managing this NPC.*

  • public override void Exit(AIController controller)

    Called when the state is exited. No special logic needed for retreat state. Parameters: - AIController controller: The AI controller managing this NPC.*

  • public override void UpdateState(AIController controller, float deltaTime)

    Called every frame while in this state. Handles retreat logic and transitions when safe distance is reached. Parameters: - AIController controller: The AI controller managing this NPC. - float deltaTime: Time since last update.*


Basic Usage

Setup

  1. Create a new RetreatState asset via the Unity Editor (Assets > Create > FishMMO > Character > NPC > AI > Retreat State).
  2. Assign the state to an AIController for NPCs that require retreating behavior.
  3. Adjust RetreatDistance and SafeDistance as needed for your NPC behavior.

Example

// Example 1: Assigning the Retreat state to an AI controller
AIController ai = ...;
RetreatState retreatState = ...;
ai.ChangeState(retreatState);

// Example 2: Customizing retreat parameters
retreatState.RetreatDistance = 15.0f;
retreatState.SafeDistance = 25.0f;

Best Practices

  • Adjust RetreatDistance and SafeDistance for different NPC types and retreat behaviors.
  • Use NavMesh sampling to ensure valid retreat destinations.
  • Transition to appropriate states when the target is lost or safe distance is reached.
  • Document custom AI state logic for maintainability and clarity.
⚠️ **GitHub.com Fallback** ⚠️