A* Pathfinding Project Module
This module enables unit pathfinding to use the A* Pathfinding Project asset.
Features
Taps into all the features that the pathfinding asset offers including grid graphs, navigation meshes, obstacle avoidance and crowd control allowing the RTS Engine units to make use of all of those features.
Requirements
- RTS Engine 2022.0.0 or higher
- A* Pathfinding Project 4.2.17 or higher.
Installation
- Import the RTS Engine (or back up your project if you already have it imported).
- Download A* Pathfinding Project and import it into your project.
- Get the A* Pathfinding Project Module and import it into your project.
Guide
In this guide, we will go through setting up map scenes and entity prefabs using two graph types from the pathfinding asset: Grid and Navmesh graphs.
1. Grid Graph
The scene a_star_grid_demo_map on the path: RTS Engine -> Modules -> AStarPathfindingProject -> Scenes provides an example where the grid graph is used to handle pathfinding where there are two grid graphs used, one for the ground terrain and one for the air terrain as well as a building prefab and two unit prefabs: one that moves on the ground terrain and one flying unit.
1.1 Setup Scene
In a map scene, do the following:
Follow the A* Pathfinding Project docs to create a Grid graph.
In the hierarchy, navigate to GameManager -> Essential -> Movement, remove the Nav Mesh Movement System and add the AStar Movement System.
Make sure that the terrain objects which have layers defined in the Height Testing Mask field of the Grid Graph have their colliders with Is Trigger disabled so that they are detected by the pathfinding asset. In addition, make sure you define the Obstacle Layer Mask with layers used for entity obstacles objects and non entity obstacles objects. If some of your attack entities rely on obstacle LOS, make sure that the layers defined in the obstacle LOS and the layers defned on the building obstacle objects are different to avoid issues where attack entities are not able to launch attacks on buildings since grid graph obstacles require an active collider component to function.
1.2 Setup Unit Prefab
Open a unit prefab and do the following:
Remove the Nav Mesh Agent component from the unit prefab main object.
Add the A* Seeker component to the unit prefab main object.
Add one of the A* movement scripts to the unit prefab main object. Both the seeker and this component are replacing the Nav Mesh Agent component.
- More information on the movement scripts.
- For this setup, we will be using the AI Path component. Make sure to set the Gravity field to None in the AI Path component.
Navigate to the child game object of the unit prefab which has the Unit Movement movement, on the same object there should be the Unity Navmesh's default Nav Mesh Agent Controller component. Remove the latter component and add the AStar AI Path Controller component instead. If you are using Rich AI instead of AI Path, you want to add the AStar Rich AI Path Controller component instead.
1.3 Setup Building Prefab
Open a building (or a resource building) prefab and do the following:
Remove the instances of the default Nav Mesh Obstacle and Nav Mesh Entity Obstacle components attached to the main building prefab object or its child objects.
Replace the latter with the combination of AStar Grid Entity Obstacle and Dynamic Grid Obstacle components with the latter component being disabled by default.
The Dynamic Grid Obstacle requires a Collider component to be attached on the same object with Is Trigger disabled. In Unity's Navmesh, the Nav Mesh Obstacle itself includes a collider but in the case of this asset when using a Grid graph, we need to explicitly define a Collider component to define the obstacle's constraints.
Make sure that the layer of the object that has the obstacle components is defined in the Grid graph obstacles layer mask.
1.3 Setup Resource Prefab
Open a resource prefab and do the following:
- Remove the instances of the default Nav Mesh Obstacle component attached to the main building prefab object or its child objects.
- Replace the latter with the Dynamic Grid Obstacle component and make sure it is enabled by default.
- The Dynamic Grid Obstacle requires a Collider component to be attached on the same object with Is Trigger disabled. In Unity's Navmesh, the Nav Mesh Obstacle itself includes a collider but in the case of this asset when using a Grid graph, we need to explicitly define a Collider component to define the obstacle's constraints.
- Make sure that the layer of the object that has the obstacle components is defined in the Grid graph obstacles layer mask.
2. Navmes/Recast Graph
The scene a_star_navmesh_demo_map on the path: RTS Engine -> Modules -> AStarPathfindingProject -> Scenes provides an example where the navmesh/recast graph is used to handle pathfinding where there is a navmesh used for the ground terrain as well as a building prefab and a unit prefab.
1.1 Setup Scene
In a map scene, do the following:
Follow the A* Pathfinding Project docs to create a Navmesh/Recast graph.
In the hierarchy, navigate to GameManager -> Essential -> Movement, remove the Nav Mesh Movement System and add the AStar Movement System.
Make sure that the terrain objects which have layers defined in the Layer Mask field of the Recast Graph have their colliders with Is Trigger disabled so that they are detected by the pathfinding asset.
1.2 Setup Unit Prefab
Open a unit prefab and do the following:
Remove the Nav Mesh Agent component from the unit prefab main object.
Add the A* Seeker component to the unit prefab main object.
Add one of the A* movement scripts to the unit prefab main object. Both the seeker and this component are replacing the Nav Mesh Agent component.
- More information on the movement scripts.
- For this setup, we will be using the Rich AI component which is recommended to use alongside navmesh based graphs. Make sure to set the Gravity field to Use Project Settings and then set the Raycast Ground Mask field to the layers used by the terrain objects that the unit can move on.
Navigate to the child game object of the unit prefab which has the Unit Movement movement, on the same object there should be the Unity Navmesh's default Nav Mesh Agent Controller component. Remove the latter component and add the AStar Rich AI Path Controller component instead. If you are using Rich AI instead of AI Path, you want to add the AStar AI Path Controller component instead.
1.3 Setup Building Prefab
Open a building (or a resource building) prefab and do the following:
Remove the instances of the default Nav Mesh Obstacle and Nav Mesh Entity Obstacle components attached to the main building prefab object or its child objects.
Replace the latter with the combination of AStar Navmesh Entity Obstacle and Navmesh Cut components with the latter component being disabled by default.
Unlike in grid graphs, the Navmesh Cut does not require a Collider component to be attached on the same object.
1.3 Setup Resource Prefab
Open a resource prefab and do the following:
Remove the instances of the default Nav Mesh Obstacle component attached to the main resource prefab object or its child objects.
Replace the latter with the combination of AStar Navmesh Entity Obstacle and Navmesh Cut components with the latter component being disabled by default.
Unlike in grid graphs, the Navmesh Cut does not require a Collider component to be attached on the same object. However, for resource entities it does require the AStar Navmesh Entity Obstacle component to be attached as well.
v2022.3.0
Release Date: 08/25/2022
Requirements
- RTS Engine 2022.3.0 or higher
Changes:
- FIX a bug in Navmesh/Recast graph based pathfinding where the removal/addition of a new building or resource entity that has a NavmeshCut component makes any unit's movement stuck when the unit is within a certain range of the added/removed building or resource entity.
- UPDATE demo scenes for both Navmesh/Rect and Grid based pathfinding with entities from the demo game.
v2023.0.0
Release Date: 05/10/2023
Requirements
- RTS Engine 2023.0.0 or higher
Changes:
- UPDATE grid graph demo scene to use a smaller size for nodes and updated the building and resource obstacle entity child objects layer and size.
- ADD special layer for A* Grid based obstacles.
v2023.0.1
Release Date 06/27/2023
Requirements
- RTS Engine 2023.0.1 or higher
Changes:
- FIX a bug that resets the original path destination when there is no valid path to the original path destination and another destination around it must be targeted. This occurs when instructing a unit to move towards a building or a resource where the unit is supposed to move to the interaction position of the target object.
- UPDATE Recast and Grid graph demo scenes to have examples of collecting and dropping off resources.