Skip to content
Pablo Rodriguez

Ai Combat System

Create specialized enemy types by organizing AI characters into weapon-specific child classes:

  • Base Class: bp_character_coursera_ai_master
  • Shared Systems: Combat state, health, movement, detection
  • Override Functions: Attack behavior, weapon configuration

Create child blueprints for each weapon type:

  • bp_ai_shield: Sword and shield combat AI
  • bp_ai_rifle: Ranged firearm combat AI
  • bp_ai_bow: Archer combat AI
  • bp_ai_mage: Magic-based combat AI
  • bp_ai_dagger: Stealth/fast attack AI
  • bp_ai_two_handed: Heavy weapon combat AI

Configure each enemy type with appropriate weapon stance:

// AI Animation Stance Configuration
F_AI_Set_Animation_Stance() {
mesh.GetAnimInstance().Interface_Animation_Stance(Animation_Stance);
}
// Child class defaults:
// Shield AI: Animation_Stance = Shield
// Rifle AI: Animation_Stance = Rifle
// Bow AI: Animation_Stance = Bow
// etc.

Create attack system that adapts to different weapon types:

  • F_AI_Attack: Empty function in master class for child overrides
  • Target System: AI maintains reference to current attack target
  • Rotation: AI faces target before attacking
// AI Target Management
OnPlayerAggro(Player) {
Target = Player;
LookAtTarget();
}
OnResetCharacter() {
Target = nullptr; // Clear target when resetting
}

Control when AI characters can attack:

  • Attack_Range Variable: Distance required to initiate attack (100-200 units for melee, 1000+ for ranged)
  • Attack_Speed Variable: Cooldown between attacks (3 seconds default)
  • Do Once Node: Prevent attack spam
// AI Attack Validation
CE_Check_Attack_Range() {
if (Target.IsValid() && !Dead && !Reset) {
distance_to_target = Distance(GetActorLocation(), Target.GetActorLocation());
if (distance_to_target <= Attack_Range) {
if (!Target.Interface_Get_Dead_State()) {
F_AI_Attack(); // Execute attack
StartAttackCooldown(Attack_Speed);
} else {
CE_Reset_AI_Character(); // Target died, reset AI
}
} else {
AI_Move_To(Target.Location, Target, true); // Move closer
}
}
}

For sword, dagger, and two-handed enemies:

  • Weapon_Hitbox: Box collision for weapon damage
  • Socket Attachment: Attach to appropriate weapon socket
  • Collision Management: Enable during attack animations, disable otherwise

Copy player attack code and adapt for AI:

  • Animation Montages: Use same attack animations as player
  • Damage Values: Set appropriate damage for enemy type
  • Timing: Use montage notify system for precise hit timing

For bow, rifle, and magic enemies:

  • Spawn Projectile: Use existing projectile system
  • Socket Locations: Weapon-appropriate spawn points (muzzle, arrow socket, hand)
  • Range Advantage: Higher attack range (1000 units)
  • Movement Distance: Stay at range (900-unit move-to radius)
// Ranged AI Attack Example
// Magic AI Attack
F_AI_Attack_Override() {
PlayMontage(MagicAttack01);
// Montage notify triggers projectile spawn
}

Create more complex enemies with multiple attacks:

  • Random Integer: Generate random number for attack choice
  • Switch Node: Route to different attack animations
  • Attack Variety: 3-4 different attacks per boss-level enemy
// Attack Rotation System
F_AI_Attack_Boss() {
Attack_Index++;
if (Attack_Index > 3) {
Attack_Index = 1; // Reset to first attack
}
switch(Attack_Index) {
case 1: PlayMontage(TwoHandedAttack01); break;
case 2: PlayMontage(TwoHandedAttack02); break;
case 3: PlayMontage(TwoHandedAttack03); break;
}
}

Ensure AI attacks properly register hits:

  • Hitbox Override: Override F_Get_Melee_Hitbox function in each child class
  • Collision Events: Implement overlap events for AI weapon hitboxes
  • Player Targeting: Only damage player characters, not other AI
  • Attack Movement: Stop movement during attack animations
  • Range Maintenance: Ranged AI maintain distance from player
  • Positioning: Melee AI close distance for attack opportunities

Ensure AI respects team allegiance:

  • Team Variable: Set AI team to “Enemy”
  • Player Team: Set player team to “Player”
  • Projectile Integration: AI projectiles only damage player team
  • Friendly Fire Prevention: AI don’t damage each other

The AI combat system creates varied, challenging enemy encounters while maintaining clear, predictable behavior patterns that enhance gameplay rather than frustrate players.