AI Following the Player in Unreal Engine 5

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.

All blueprint nodes from the previous step.

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.

Deleting the GetActorLocation and GetRandomReachablePointInRadius nodes

Next, add a new “Get Player Character” node to the blueprint graph.

Adding the Get Player Character node

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.

Connecting the Get Player Character node to the Target Actor pin of the AI MoveTo node

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.

Connecting the On Fail pin to the Delay node

Lastly, make sure to Compile and Save the blueprint editor. This will ensure the changes are applied when we play in the editor.

Compile and save the blueprint and actor

Use Cases

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:

Demo Video

The AI following the player character around the Unreal Engine level.

Download the Project Files

Conclusion

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.

Further Reading:
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

Be the first to comment

Leave a Reply

Your email address will not be published.


*