ReturnHomeState - jimdroberts/FishMMO GitHub Wiki
AI State for returning the NPC to its home position. Handles healing and movement speed adjustments. This state is responsible for guiding the NPC back to its designated home location, restoring health if configured, and managing speed changes during the return process. It ensures the NPC disengages from combat and resumes normal behavior upon arrival.
-
public bool CompleteHealOnReturn
If true, the NPC will be fully healed upon returning home.
-
public override void Enter(AIController controller)
Called when the state is entered. Sets the NPC's destination to home, increases speed, and heals if applicable. Parameters:
-
controller
(AIController): The AI controller managing this NPC.
-
-
public override void Exit(AIController controller)
Called when the state is exited. Resets movement speed and optionally disables immortality. Parameters:
-
controller
(AIController): The AI controller managing this NPC.
-
-
public override void UpdateState(AIController controller, float deltaTime)
Called every frame while in this state. Checks if the NPC has reached its home destination and transitions to random movement. Parameters:
-
controller
(AIController): The AI controller managing this NPC. -
deltaTime
(float): Time since last update.
-
- Ensure the NPC GameObject has an AIController component and is configured with a home position.
- Attach the ReturnHomeState ScriptableObject to the NPC's AI state machine.
- Set the
CompleteHealOnReturn
field as desired in the Inspector. - The state will be triggered by the AI system when the NPC needs to return home.
// Example 1: Returning an NPC to its home position
// Assume 'controller' is a reference to the AIController for the NPC.
// Enter the ReturnHomeState
returnHomeState.Enter(controller);
// ...in the game loop or AI update...
returnHomeState.UpdateState(controller, Time.deltaTime);
// When the NPC arrives home, Exit is called automatically by the AI system.
returnHomeState.Exit(controller);
- Use
CompleteHealOnReturn
to quickly restore NPC health after combat or patrol. - Ensure the NPC's home position is set and accessible to avoid navigation issues.
- Consider customizing the speed values in the Constants class for different NPC types.
- If using immortality during return, ensure it is properly reset in Exit to avoid unintended invulnerability.