Easy Dynamic Pathfinding in Unreal Engine 4

Unreal Engine 4’s pathfinding and navigation system gives all users a robust and easy to use system for adding AI controlled characters into their games. With the default settings, navigation is static preventing changes at runtime.

Dynamically generating the nav mesh (Navigation Mesh) and adjusting it during runtime has very little documentation which is why this guide has been created.

In this guide we will be working through how to enable runtime navmesh generation and how to affect the navmesh with navigation modifiers.

Switching to Runtime Generation

Firstly, to enable this feature open up your Project Settings.

Project settings option

From here find and click the Navigation Mesh tab under the Engine category.

Navigation Mesh settings

In the Navigation Mesh settings set the Runtime Generation setting to Dynamic.

Setting the runtime generation to dynamic

Adding Navigation Modifiers

Any actor component that has collision can be setup to cut holes inside the navigation mesh.

Obstacle now changing the nav mesh

To enable this feature, I created a new actor named NavigationBlocker.

This actor stops AI controlled actors from moving in the red area that the collision occupies.

Furthermore, the NavigationBlocker moves around the level, regenerating the navmesh to reflect the changed environment.

To keep it simple, this actor only has a Box Collision.

NavigationBlocker actor

Click the box collision component from the components list (shown above) and find the navigation category in the details panel on the left.

Tick the Dynamic Obstacle checkbox and set the area class to obstacle.

Navigation settings

Area Class

The area class determines how AI controlled actors will react to the modifier.

  • None will not affect the navigation.
  • Default will act like a normal area of navmesh.
  • LowHeight blocks all AI from navigating through it due to the height restriction.
  • Null blocks all navigation inside it.
  • Obstacle tells the AI that this area can be navigated through but only if there are no other possible ways to move to the target.

Example

The AI actor now dynamically adjusts to walk around the box.

To download the project files for this tutorial click below:

Conclusion

With this setup your AI controlled actors will now react to the changing environments.

This system is essential for creating city building or strategy games.

A few examples of games that would use an equivalent system would be Age of Empires, Starcraft and Command and Conquer.

3 Comments

  1. This works in small, very small levels, but anything the size of a production level will struggle. It appears especially bad when areas stream in, as the nav has to update hundreds of props worth of nav.

    Its probably possible to create a blocking volume pass that place-holds many of the to-be streamed props, but that is a huge undertaking for such little gain.

    Ideally a simple dynamic volume that could be placed in a BP would solve the issue.

    Interested to hear thoughts.

Leave a Reply

Your email address will not be published.


*