Class BaseAIState
Abstract base class for all AI states. Defines common parameters and logic for state transitions, leash checks, enemy detection, and line of sight.
public abstract class BaseAIState : CachedScriptableObject<BaseAIState>, ICachedObject
- Inheritance
-
ObjectScriptableObjectBaseAIState
- Implements
- Derived
- Inherited Members
-
ScriptableObject.SetDirty()ScriptableObject.CreateInstance<T>()Object.GetEntityId()Object.GetInstanceID()Object.GetHashCode()Object.InstantiateAsync<T>(T)Object.InstantiateAsync<T>(T, Transform)Object.InstantiateAsync<T>(T, Vector3, Quaternion)Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Scene)Object.Instantiate<T>(T, InstantiateParameters)Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.FindObjectsByType<T>()Object.FindObjectsByType<T>(FindObjectsInactive)Object.ToString()Object.nameObject.hideFlags
Fields
DetectionRadius
The radius within which the AI will detect enemies.
public float DetectionRadius
Field Value
EnemyLayers
The layers considered as enemies for detection.
public LayerMask EnemyLayers
Field Value
- LayerMask
KeepsCombatTarget
[Header("Combat Continuity")]
[Tooltip("This state continues the current fight (orbit/flank/kite). Keeps the combat target when entered from an attacking state.")]
public bool KeepsCombatTarget
Field Value
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.
[Tooltip("The rate at which the AI will check if it should leash and return home. If 0 or below it will never leash.")]
public float LeashUpdateRate
Field Value
LineOfSightBlockingLayers
The layers that block line of sight checks.
public LayerMask LineOfSightBlockingLayers
Field Value
- LayerMask
MaxLeashRange
Maximum distance at which the AI will forget the current target and teleport home.
public float MaxLeashRange
Field Value
MinLeashRange
Minimum distance at which the AI will forget the current target and return home.
public float MinLeashRange
Field Value
Methods
Enter(AIController)
Called when the state is entered. Implement state-specific logic here.
public abstract void Enter(AIController controller)
Parameters
controllerAIControllerThe AI controller managing this NPC.
Exit(AIController)
Called when the state is exited. Implement cleanup logic here.
public abstract void Exit(AIController controller)
Parameters
controllerAIControllerThe AI controller managing this NPC.
GetUpdateRate()
Returns the update rate for this state (in seconds).
public virtual float GetUpdateRate()
Returns
GetUpdateRate(AIController)
Returns the update rate for this state, given the NPC that is running it.
public virtual float GetUpdateRate(AIController controller)
Parameters
controllerAIControllerThe AI controller running this state.
Returns
- float
Update rate in seconds.
Remarks
States that randomise their tick interval need the owning NPC's seeded RNG to stay deterministic. IdleState and WanderState previously reached for Shared here, which made their timing depend on how many other NPCs had drawn from the shared stream first.
HasLineOfSight(AIController, ICharacter)
Checks if the AI has line of sight to the target. Uses raycasting to determine if any objects block the view.
public virtual bool HasLineOfSight(AIController controller, ICharacter target)
Parameters
controllerAIControllerThe AI controller managing this NPC.
targetICharacterThe target character to check line of sight to.
Returns
- bool
True if line of sight exists, false otherwise.
RandomizeRate(AIController, float, float)
Returns a value between minimum and maximum using
the NPC's seeded RNG, or minimum when the range is degenerate.
protected static float RandomizeRate(AIController controller, float minimum, float maximum)
Parameters
controllerAIControllerThe AI controller running this state.
minimumfloatLower bound.
maximumfloatUpper bound. Values at or below the lower bound return the lower bound.
Returns
- float
The randomised rate.
ReturnToCombatOrIdle(AIController)
Returns the NPC to its attacking state when it still has a live target, or to idle when it does not.
protected static void ReturnToCombatOrIdle(AIController controller)
Parameters
controllerAIControllerThe AI controller managing this NPC.
Remarks
Combat sub-states (orbit, flank) used to hard-code a transition to idle when their manoeuvre completed, which dropped the NPC out of a fight it was still winning.
SweepForEnemies(AIController, List<ICharacter>)
Sweeps for nearby enemies using physics overlap checks and faction logic. Populates the provided list with detected enemies. Returns true if any enemies are detected with line of sight.
public virtual bool SweepForEnemies(AIController controller, List<ICharacter> detectedEnemies)
Parameters
controllerAIControllerThe AI controller managing this NPC.
detectedEnemiesList<ICharacter>Reusable list to populate with detected enemy characters. Cleared before use.
Returns
- bool
True if any enemies are detected, false otherwise.
UpdateState(AIController, float)
Called every frame while in this state. Implement state update logic here.
public abstract void UpdateState(AIController controller, float deltaTime)
Parameters
controllerAIControllerThe AI controller managing this NPC.
deltaTimefloatTime since last update.