RetreatState - jimdroberts/FishMMO GitHub Wiki
AI State for retreating from a target. Causes the NPC to move away from its target until a safe distance is reached.
-
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.
-
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.*
- Create a new RetreatState asset via the Unity Editor (Assets > Create > FishMMO > Character > NPC > AI > Retreat State).
- Assign the state to an AIController for NPCs that require retreating behavior.
- Adjust RetreatDistance and SafeDistance as needed for your NPC behavior.
// 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;
- 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.