BaseAIState - jimdroberts/FishMMO GitHub Wiki

Description

Abstract base class for all AI states. Defines common parameters and logic for state transitions, leash checks, enemy detection, and line of sight. Provides a foundation for implementing specific AI behaviors in FishMMO.


API Access

Fields

  • public float LeashUpdateRate

    The rate (in seconds) at which the AI will check if it should leash and return home. If 0 or below, leashing is disabled.

  • public float MinLeashRange

    Minimum distance at which the AI will forget the current target and return home.

  • public float MaxLeashRange

    Maximum distance at which the AI will forget the current target and teleport home.

  • public float DetectionRadius

    The radius within which the AI will detect enemies.

  • public LayerMask EnemyLayers

    The layers considered as enemies for detection.

  • public LayerMask LineOfSightBlockingLayers

    The layers that block line of sight checks.

Properties

  • public virtual float GetUpdateRate()

    Returns the update rate for this state (in seconds). Returns: float — Update rate in seconds.

Methods

  • public abstract void Enter(AIController controller)

    Called when the state is entered. Implement state-specific logic here. Parameters:

    • controller (AIController): The AI controller managing this NPC.*
  • public abstract void Exit(AIController controller)

    Called when the state is exited. Implement cleanup logic here. Parameters:

    • controller (AIController): The AI controller managing this NPC.*
  • public abstract void UpdateState(AIController controller, float deltaTime)

    Called every frame while in this state. Implement state update logic here. Parameters:

    • controller (AIController): The AI controller managing this NPC.
    • deltaTime (float): Time since last update.*
  • public virtual bool HasLineOfSight(AIController controller, ICharacter target)

    Checks if the AI has line of sight to the target. Uses raycasting to determine if any objects block the view. Parameters:

    • controller (AIController): The AI controller managing this NPC.
    • target (ICharacter): The target character to check line of sight to.* Returns: bool — True if line of sight exists, false otherwise.
  • public virtual bool SweepForEnemies(AIController controller, out List detectedEnemies)

    Sweeps for nearby enemies using physics overlap checks and faction logic. Returns true if any enemies are detected with line of sight. Parameters:

    • controller (AIController): The AI controller managing this NPC.
    • detectedEnemies (out List): List of detected enemy characters.* Returns: bool — True if any enemies are detected, false otherwise.

Basic Usage

Setup

  1. Create a new ScriptableObject class that inherits from BaseAIState for each specific AI behavior.
  2. Implement the required abstract methods: Enter, Exit, and UpdateState.
  3. Assign the custom AI state ScriptableObjects to the AIController's state fields in the Inspector.
  4. Configure leash, detection, and line of sight parameters as needed for each state.

Example

// Example 1: Creating a custom AI state
public class CustomChaseState : BaseAIState
{
    public override void Enter(AIController controller)
    {
        // Custom logic for entering chase state
    }

    public override void Exit(AIController controller)
    {
        // Cleanup logic
    }

    public override void UpdateState(AIController controller, float deltaTime)
    {
        // State update logic
    }
}

Best Practices

  • Use LeashUpdateRate, MinLeashRange, and MaxLeashRange to control how and when NPCs return home.
  • Adjust DetectionRadius and EnemyLayers to fine-tune enemy detection for different AI types.
  • Always implement the abstract methods to define clear state transitions and behaviors.
  • Use HasLineOfSight and SweepForEnemies to build robust enemy detection and targeting logic.
⚠️ **GitHub.com Fallback** ⚠️