AIController - jimdroberts/FishMMO GitHub Wiki

Description

Controls AI navigation, state transitions, and behavior for NPCs using NavMeshAgent. Handles movement, enemy detection, leash logic, waypoints, and state management for non-player characters in FishMMO.


API Access

Fields

  • public Collider[] SweepHits

    Buffer for storing colliders hit during enemy sweep.

  • public float EnemySweepRate

    How often (in seconds) to sweep for nearby enemies.

  • public BaseAIState InitialState

    The initial AI state when the controller is started.

  • public AgentAvoidancePriority AvoidancePriority

    The avoidance priority for this agent (affects how strongly it avoids other agents).

  • public BaseAIState WanderState

    Reference to the wander state for random movement.

  • public BaseAIState PatrolState

    Reference to the patrol state for waypoint movement.

  • public BaseAIState ReturnHomeState

    Reference to the return home state for leash logic.

  • public BaseAIState RetreatState

    Reference to the retreat state for fleeing behavior.

  • public BaseAIState IdleState

    Reference to the idle state for passive behavior.

  • public BaseAIState AttackingState

    Reference to the attacking state for combat behavior.

  • public BaseAIState DeadState

    Reference to the dead state for death logic.

  • public Transform LookTarget

    The current look target for the AI (used for facing/rotation).

  • public bool RandomizeState

    If true, the AI will randomize its movement state.

  • public Vector3[] Waypoints

    The waypoints available to this AI controller.

Properties

  • public Transform EyeTransform

    The transform used for vision checks. Defaults to the character's transform if not set.

  • public PhysicsScene PhysicsScene { get; private set; }

    The physics scene associated with this AI controller.

  • public Vector3 Home { get; set; }

    The home position for this AI (used for leash and wandering).

  • public Transform Target { get; set; }

    The current target for the AI (e.g., enemy, destination). Setting this property updates the agent's destination.

  • public NavMeshAgent Agent { get; private set; }

    The NavMeshAgent component used for navigation.

  • public BaseAIState CurrentState { get; private set; }

    The current AI state.

  • public int CurrentWaypointIndex { get; private set; }

    The current waypoint index.

Methods

  • public override void OnStartNetwork()

    Called when the network starts. Disables the controller if not running on the server.

  • public override void InitializeOnce()

    Initializes the controller and NavMeshAgent. Sets avoidance priority, speed, and movement states.

  • public void Initialize(Vector3 home, Vector3[] waypoints = null)

    Initializes the controller with a home position and waypoints. Sets agent dimensions and initial state. Parameters:

    • home (Vector3): The home position for the AI.
    • waypoints (Vector3[], optional): Optional waypoints for patrol.*
  • public override void ResetState(bool asServer)

    Resets the controller's state, clearing home, target, and look target. Parameters:

    • asServer (bool): Whether the reset is performed on the server.*
  • void Update()

    Unity Update loop. Handles enemy sweeping, leash checks, state updates, and facing look target.

  • private void SweepForEnemies()

    Sweeps for nearby enemies and transitions to attacking state if any are found.

  • private void CheckLeash()

    Checks leash distance and transitions to return home or warps home if leash is exceeded.

  • private void UpdateCurrentState()

    Updates the current state if needed, calling its UpdateState method.

  • public void Stop()

    Stops the agent's movement.

  • public void Resume()

    Resumes the agent's movement.

  • public void ChangeState(BaseAIState newState, List targets = null)

    Changes the AI state, optionally providing targets for attacking states. Handles speed and state transitions. Parameters:

    • newState (BaseAIState): The new state to transition to.
    • targets (List, optional): Optional list of targets for attacking states.*
  • public void TransitionToIdleState()

    Transitions to the idle state.

  • public virtual void TransitionToRandomMovementState()

    Transitions to a random movement state from the available movement states.

  • public void SetRandomHomeDestination(float radius = 5.0f)

    Sets a random destination within a radius around the home position. Parameters:

    • radius (float, optional): Radius to randomize destination.*
  • public void SetRandomDestination(float radius = 5.0f)

    Sets a random destination within a radius around the current position. Parameters:

    • radius (float, optional): Radius to randomize destination.*
  • public void TransitionToNextWaypoint()

    Transitions to the next waypoint in the waypoint array.

  • public void PickNearestWaypoint()

    Picks the nearest waypoint to the current position and sets it as the destination.

  • public void FaceLookTarget()

    Rotates the character to face the current look target smoothly.


Basic Usage

Setup

  1. Attach the AIController component to an NPC GameObject with a NavMeshAgent.
  2. Assign the required AI states (Wander, Patrol, ReturnHome, Retreat, Idle, Attacking, Dead) in the Inspector.
  3. Set the Home position and optional waypoints for the NPC.
  4. Configure avoidance priority and sweep rates as needed.
  5. Ensure the NavMesh is properly baked for navigation.

Example

// Example 1: Initializing and using the AIController
// Assume 'controller' is a reference to the AIController for the NPC.

// Initialize the controller with a home position and waypoints
controller.Initialize(new Vector3(0, 0, 0), waypointsArray);

// In the game loop or update method
controller.Update();

// To change the AI state manually
controller.ChangeState(controller.WanderState);

Best Practices

  • Assign all relevant AI states to enable full NPC behavior.
  • Use waypoints for patrol patterns and set a reasonable home position for leash logic.
  • Adjust sweep rates and avoidance priorities for different NPC types.
  • Always ensure the NavMeshAgent is configured and the NavMesh is baked for the scene.
  • Use the provided methods to control movement and state transitions programmatically.
⚠️ **GitHub.com Fallback** ⚠️