BaseAIState - jimdroberts/FishMMO GitHub Wiki
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.
-
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.
-
public virtual float GetUpdateRate()
Returns the update rate for this state (in seconds). Returns: float — Update rate in seconds.
-
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.
-
- Create a new ScriptableObject class that inherits from
BaseAIState
for each specific AI behavior. - Implement the required abstract methods:
Enter
,Exit
, andUpdateState
. - Assign the custom AI state ScriptableObjects to the AIController's state fields in the Inspector.
- Configure leash, detection, and line of sight parameters as needed for each state.
// 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
}
}
- Use
LeashUpdateRate
,MinLeashRange
, andMaxLeashRange
to control how and when NPCs return home. - Adjust
DetectionRadius
andEnemyLayers
to fine-tune enemy detection for different AI types. - Always implement the abstract methods to define clear state transitions and behaviors.
- Use
HasLineOfSight
andSweepForEnemies
to build robust enemy detection and targeting logic.