One common feature of many modern games is AI characters or enemies chasing or following the player. Luckily adding this functionality is incredibly easy and only takes a few blueprint nodes.
In this guide we are adding the ability for AI Following the Player in Unreal Engine 5.
Before we Start
This guide continues on from the CouchLearn guide “Setting up a NavMesh in Unreal Engine 5”.
Our previous guide explains how to set up a basic NavMesh and AI character in your Unreal Engine 5 project. Follow the previous guide found here or download the project files here.
Chasing the Player
In the previous “Setting up a NavMesh in Unreal Engine 5”. guide, we finished off with these blueprint nodes to move the AI character around the level.
Firstly, delete the “Get Actor Location” and “GetRandomReachablePointInRadius” nodes from the graph.
These nodes are currently trying to find a random position around the AI. We want the AI to use the player’s location so it is safe to delete these two nodes.
Next, add a new “Get Player Character” node to the blueprint graph.
Then, connect the blue “Return Value” pin to the blue “Target Actor” pin on the “AI MoveTo” node.
This will now get our “Target Actor’s” location for the AI movement.
As our AI might fail to move, if the player isn’t reachable in the navmesh, we now connect the “On Fail” pin to the “Delay” node.
This now retries the movement if the player is unreachable or the AI fails the nav mesh movement for any reason. This step will keep the AI following the player no matter the situation.
Lastly, make sure to Compile and Save the blueprint editor. This will ensure the changes are applied when we play in the editor.
Even thought these steps are easy and only a few blueprint nodes are used, this basic system can enable many types of AI in modern games.
A few examples of this could be:
- Companion AI following the player around an open world like Skyrim (Using the Unreal Engine dynamic open world navigation).
- Zombie AI chasing the player (Using the Unreal Engine damage system).
Download the Project Files
Now your AI character / enemy is following and chasing the player character around the level in Unreal Engine 5!
As explained in the previous step, this basic system can be expanded to implement many modern game AI behaviours into your Unreal Engine project. To learn more about the capabilities of AI and Navigation in Unreal Engine, consider reading further using the links below.
How to smoothly rotate AI characters in Unreal Engine
Open world navigation in Unreal Engine
Dynamically generated navigation in Unreal Engine
Official Unreal Engine Navigation documentation