I'm looking forward to use A* pathfinding for a game I'm working on. (I'm actually making a game for myself to learn about this). I am wondering how the Unity NavMesh can be used with a custom A* algorithm, instead of using a NavMeshAgent.
No
Or at least, not easily (why would you want to?).
Unity's builtin NavMesh is intended to be used by Unity's builtin NavMeshAgent utilizing a builtin pathfinder. I don't know what algorithm it uses, but A* implementations typically operate on networks. That is, nodes connected by edges. It does not consider the interior volume (the mesh 'faces').
As Unity's builtins are intended to be used as such, it is very difficult to get access to any of the information directly for use with your own pathfinding algorithms.
If you want to write your own pathfinder, then I recommend writing your own mesh as well.
You can do the following:
var navMesh = NavMesh.CalculateTriangulation() // get baked Navigation Mesh Data;
Vector3[] vertices = navMesh.vertices;
int[] polygons = navMesh.indices;
vertices (obviously) are the vertices of your navigation mesh indicated by their position in unity space. The meshes are defined by polygons. The polygon array shows which vertice belongs to which polygon.
polygons:
0 0
1 0
2 1
3 1
4 0
5 1
This array would indicate that vertices with indices 0,1,4 belongs to polygon number 0 and vertices with indices 2,3,5 belongs to polygon number 1.
There you have your navmesh as polygons. You can define your search graph and run the search algorithm of your choice.
© 2022 - 2024 — McMap. All rights reserved.