Smart Enemy AI | (Part 8: Player Attack & Enemy Block) | Tutorial in Unreal Engine 5 (UE5)

4 min read 1 month ago
Published on Apr 03, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Introduction

In this tutorial, we will build the player attack system and enemy block functionality within Unreal Engine 5. This guide is part of a larger series focused on creating intelligent enemy AI. By the end of this tutorial, you will have a shared attack system for both the player and enemies, implement camera aiming and shooting, and enable melee enemies to block incoming attacks.

Step 1: Set Up Player Attack Stance

  1. Create Player Attack Animations

    • Import the required attack animations into your project.
    • Ensure that these animations are compatible with your character's skeleton.
  2. Implement Player Attack Logic

    • In your player character blueprint, define a new function for the attack.
    • Add input mappings for the attack action in the project settings.
    • Use the following code structure to trigger the attack animation:
      InputAction Attack
      → Play Animation (Attack Animation)
      
  3. Set Attack Hitboxes

    • Create a collision box in the player’s attack animation state.
    • Ensure it activates during the attack animation to detect hits on enemies.

Step 2: Create Crosshair Widget

  1. Design the Crosshair

    • Create a new widget blueprint for the crosshair.
    • Use an image component to display the crosshair graphic.
  2. Add Functionality to the Crosshair

    • Bind the visibility of the crosshair to the player’s aiming state.
    • Use the following logic to toggle the crosshair when aiming:
      If Is Aiming
      → Set Visibility (Crosshair Widget) to Visible
      Else
      → Set Visibility to Hidden
      

Step 3: Implement Player and Enemy Attack System

  1. Define Attack Properties

    • Create variables for attack damage and range in both player and enemy blueprints.
    • Ensure that these properties can be adjusted in the editor for testing.
  2. Create Attack Functionality for Enemies

    • Set up a similar attack function as the player within the enemy blueprint.
    • Use a timer or AI behavior tree to determine when the enemy will attack.
  3. Handle Damage Application

    • In both blueprints, create a function to apply damage upon successful hits.
    • Ensure that this function checks for overlapping hitboxes and reduces health appropriately.

Step 4: Enable Blocking Mechanics for Enemies

  1. Set Up Enemy Block State

    • Create a new boolean variable for blocking in the enemy blueprint.
    • Add an input or AI behavior to trigger the block state based on player proximity.
  2. Implement Blocking Logic

    • Modify the attack function to check if the enemy is in a blocking state.
    • If blocking is active, reduce or negate incoming damage:
      If Is Blocking
      → Damage = 0
      Else
      → Apply Damage as normal
      

Step 5: Configure Player Zoom and Aim Camera

  1. Set Up Camera Zoom

    • Adjust the camera settings in the player character blueprint to allow for zooming.
    • Use the following input mapping to trigger zoom:
      InputAction Zoom
      → Set Field of View (Camera) to Zoomed Value
      
  2. Implement Aim Functionality

    • Create an aim function that adjusts the camera position and rotation based on player input.
    • Ensure that this function properly interacts with the crosshair widget.

Conclusion

In this tutorial, we covered how to implement a player attack system, create a crosshair widget, and set up enemy blocking mechanics in Unreal Engine 5. You can enhance your game by fine-tuning the attack properties and animations as well as exploring further functionality in upcoming parts of the series. For more advanced tutorials, consider checking out the related parts linked in the video description. Happy developing!