Skip to content
Pablo Rodriguez

Ai Behavior System

Create AI aggro system for enemy detection and engagement:

  • Component: Sphere collision named “CollisionSphere_Aggro”
  • Position: At AI character feet (Z location -90)
  • Radius: 800 units for detection range
  • Collision Settings: Custom collision, ignore all, overlap pawn only

Implement player-specific detection to prevent AI attacking each other:

Create interface function to identify character types:

  • Interface_Character_Type: Returns boolean for player identification
  • Player Character: Returns true
  • AI Characters: Returns false (default)
  • NPC Characters: Returns false
// AI Aggro Detection
OnAggroSphereOverlap(OtherActor) {
if (OtherActor.Interface_Character_Type()) {
// This is the player character
F_Aggro_Player(OtherActor);
CE_SpawnLocation_Leash(); // Start leash timer
}
}

Prevent AI from wandering too far from spawn point:

  • Spawn_Location Vector: Store original spawn position on begin play
  • Leash_Distance Float: Maximum distance from spawn (default 1000 units)
  • Timer_Check_Leash_Distance: Periodic distance checking
// AI Leash System
CE_SpawnLocation_Leash() {
Timer_Check_Leash_Distance = CreateTimer(1.0, true); // Check every second
}
CE_CheckLeashDistance() {
current_distance = Distance(GetActorLocation(), Spawn_Location);
if (current_distance > Leash_Distance) {
CE_Reset_AI_Character();
}
}

Return AI to spawn location when leash exceeded:

// AI Character Reset
CE_Reset_AI_Character() {
StopMovementImmediately();
DisableAggroCollision();
AI_Move_To(Spawn_Location, false); // Move to location, not actor
// Wait for arrival before re-enabling
Timer_CheckForSpawnLocation(1.0, true);
}

Create single function for all AI movement needs:

  • Vector Input: Target location
  • Actor Input: Target actor to follow
  • Use_Actor Boolean: Choose between location and actor movement
  • Conditional Logic: Prevent movement during reset or death states
// AI Movement with Conditions
F_AI_Move_To(Location, Actor, Use_Actor) {
if (!Dead && !Reset) {
if (Use_Actor) {
MoveToActor(Actor, AcceptanceRadius);
} else {
MoveToLocation(Location, AcceptanceRadius);
}
}
}

Implement escalating AI response to player presence:

  • Player_Detection Integer: Accumulates detection level
  • Phase Thresholds: 33, 66, 100 for different behaviors
  • Value Sources: Player noise, visual contact, damage taken
// AI Detection Phases
OnDetectionUpdate(Value) {
Player_Detection += Value;
if (Player_Detection < 33) {
// Phase 0: No response
return;
} else if (Player_Detection < 66) {
// Phase 1: Look at player direction
LookAtPlayer(Player_Detected);
} else if (Player_Detection < 100) {
// Phase 2: Walk to player location
SetMoveSpeed(Walk);
AI_Move_To(Player_Detected.Location, true);
} else {
// Phase 3: Attack player
SetMoveSpeed(Run);
F_Aggro_Player(Player_Detected);
}
}

Immediate aggro when AI receives damage:

  • Last_Attacker Variable: Track who damaged this AI
  • Player Check: Verify attacker is player character
  • Immediate Aggro: Skip detection phases and go straight to combat
// AI Damage Response
F_On_Damage_Received() {
if (Last_Attacker.Interface_Character_Type()) {
// Immediate aggro on player damage
F_Aggro_Player(Last_Attacker);
}
}
  • Death Reset: Clear detection values when AI dies
  • Leash Reset: Reset detection when returning to spawn
  • Combat End: Gradually reduce detection over time

The AI behavior system creates engaging, predictable enemy encounters with proper state management and realistic response patterns that enhance gameplay without creating frustrating or confusing AI behavior.