🧠

Unreal Engine 5 - Smart Enemy AI Tutorial

Jul 11, 2024

Unreal Engine 5 - Smart Enemy AI Tutorial

Introduction

  • Series on creating smart enemy AI in Unreal Engine 5
  • Smart enemies react to environment and player actions
  • Example behaviors:
    • Hiding behind walls, attacking, and hiding again
    • Teleporting if player gets too close
    • Dodging player’s arrows
    • Healing when health is low
  • Utilizing AI tools in Unreal Engine:
    • Behavior Trees
    • Environment Query System (EQS)
    • Perception system
  • Part 1: Building a basic Behavior Tree and AI Controller
    • Enemy chases player and attacks

Getting Started

  • Using Unreal Engine 5.2 and third-person template
  • Added custom sword mesh and animation from Mixamo
  • Mixing or retargeting assets available on Patreon

Creating Player and Enemy

  1. Creating Folders: Player and Enemy
  • Create separate folders for player and multiple enemies
  1. Duplicating Character Blueprints
  • Duplicate third-person character blueprint for player and enemies
  • Move them to respective folders
  • Modify third-person game mode to use the new player blueprint
  1. Setting up the Enemy Base Blueprint
  • Duplicate player blueprint to create BP_EnemyBase
  • Remove unwanted components (camera, boom, etc.)
  • Adjust mesh and animation settings
  • Create BP_Player and assign in game mode

Implementing Basic Behavior in Blueprints

Chase Player Function

  1. Creating Custom Event: ChasePlayer
  • Use AI Move To function targeting player character
  • Set acceptance radius and add delay before repeating
  1. Setting up Begin Play
  • Call ChasePlayer on Begin Play
  1. Adding Navigation Mesh
  • Required for AI Move To function, add NavMeshBoundsVolume and scale to cover level

Attack Player Function

  1. Creating Custom Event: Attack
  • Play attack animation montage
  1. Adjusting Animation Blueprint
  • Adding functionality to disable leg IK during certain animations

Transitioning to Behavior Trees

Setting up Behavior Tree and Blackboard

  1. Creating Files
  • Create a new Behavior Tree and Blackboard (e.g., BT_EnemyBase, BP_EnemyBase)
  1. Defining Sequence and Selector
  • Setting up node types (sequence and selector)
  • Basic node: Move To targeting AttackTarget key
  1. Implementing AI Controller
  • Create and set up AIC_EnemyBase class
  • Run behavior tree and set Blackboard values in OnPossess event
  1. Debugging Behavior Tree
  • Demonstrating visual debugging in Behavior Tree editor

Implementing Custom Tasks and Decorators

  1. Creating Custom Task: Default Attack
  • Use Montage and event dispatcher for attack completion
  1. Focus on Target Task
  • Ensure AI focuses on player before attacking
  1. Clearing Focus
  • Ensure AI looks where it is moving when not attacking

Adding and Equipping Weapons

  1. Socket Setup
  • Add socket to right hand for weapon
  • Preview and adjust weapon position
  1. Spawning and Attaching Weapon
  • Create weapon blueprint (e.g., sword)
  • Spawn actor and attach in custom event
  1. Behavior Tree Integration
  • Create task to wield weapon and use decorators to manage state

Conclusion and Next Steps

  • Completed part one with basic chase and attack behaviors
  • Demonstrated using Behavior Trees and Blueprints for AI logic
  • Preview for part two: Adding perception, more complex states, etc.