BaseAttackingState - jimdroberts/FishMMO GitHub Wiki

Description

AI state for attacking behavior. Handles entering, exiting, updating, and attack logic for NPCs.


API Access

Methods

  • public override void Enter(AIController controller)

    Called when entering the attacking state. Sets agent speed to run. Parameters: - AIController controller: The AI controller.*

  • public override void Exit(AIController controller)

    Called when exiting the attacking state. Resets agent speed, clears targets, and interrupts abilities. Parameters: - AIController controller: The AI controller.*

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

    Called every frame to update the attacking state. Handles death, target loss, and attack logic. Parameters: - AIController controller: The AI controller. - float deltaTime: Frame time.*

  • public virtual void PickTarget(AIController controller, List targets)

    Picks a valid target from the provided list. Sets controller's target and look target. Parameters: - AIController controller: The AI controller. - List targets: List of potential targets.*

  • private void TryAttack(AIController controller)

    Attempts to attack the current target. Handles range checking and attack logic. Parameters: - AIController controller: The AI controller.*

  • public virtual void PerformAttack(AIController controller, ICharacter targetCharacter, float distanceToTarget, float agentRadius)

    Performs the attack on the target character. Override to implement custom attack logic. Parameters: - AIController controller: The AI controller. - ICharacter targetCharacter: The target character to attack. - float distanceToTarget: Distance to the target. - float agentRadius: Attack radius of the agent.*

  • public virtual void OutOfAttackRange(AIController controller, float distanceToTarget, float agentRadius)

    Handles logic when the target is out of attack range. Moves agent closer if possible. Parameters: - AIController controller: The AI controller. - float distanceToTarget: Distance to the target. - float agentRadius: Attack radius of the agent.*


Basic Usage

Setup

  1. Create a new BaseAttackingState asset via the Unity Editor (Assets > Create > FishMMO > Character > NPC > AI > Attacking State).
  2. Assign the state to an AIController for NPCs that require attacking behavior.
  3. Implement or override PerformAttack for custom attack logic if needed.

Example

// Example 1: Assigning the attacking state to an AI controller
AIController ai = ...;
BaseAttackingState attackingState = ...;
ai.ChangeState(attackingState);

// Example 2: Overriding PerformAttack for custom logic
public class CustomAttackingState : BaseAttackingState {
    public override void PerformAttack(AIController controller, ICharacter target, float dist, float radius) {
        // Custom attack logic here
    }
}

Best Practices

  • Always check for valid targets and character state before performing attack logic.
  • Override PerformAttack to implement specific attack behaviors for different NPC types.
  • Use PickTarget to select the most appropriate target from available enemies.
  • Document custom AI state logic for maintainability and clarity.
⚠️ **GitHub.com Fallback** ⚠️