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.

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

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

Adding Navigation Modifiers

Any actor component that has collision can be setup to cut holes inside the navigation 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.

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.

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.