A* Pathfinding Project

Unity Navmesh to A* conversion


#1

i want to convert my zombie ai script that used Unity Navmesh to A* …

so, is it possible to convert it directly?

what is equivalent of these Navmesh commands in A* ?
1- NavMeshAgent.hasPath
2- NavMeshAgent.isOnNavMesh
3- NavMeshAgent.isOnOffMeshLink
4- NavMeshAgent.isStopped
5- NavMeshAgent.path
6- NavMeshAgent.pathStatus
7- NavMeshAgent.speed
8- NavMeshAgent.SetDestination
9- NavMeshAgent.SetPath
10- NavMeshAgent Component
11- NavMeshObstacle Component


#2

Hi

IAstarAI is an interface that all movement scripts in this package implements.
See https://arongranberg.com/astar/docs/movementscripts.html

  1. IAstarAI.hasPath
  2. No corresponding property. In case of the RichAI script it is always on the navmesh if (1) is true.
  3. RichAI.traversingOffMeshLink
  4. IAstarAI.isStopped
  5. AIPath/RichAI.path (protected field)
  6. IAstarAI.pathPending
  7. IAstarAI.maxSpeed
  8. IAstarAI.destination
  9. IAstarAI.SetPath
  10. RichAI/AIPath/AILerp (see https://arongranberg.com/astar/docs/movementscripts.html)
  11. Depends on the graph type. NavmeshCut is the closest one. GraphUpdateScene is reasonable if you are using other graph types than the recast/navmesh graphs.

#3

thanks for your response :]

i used this method to spawn gameObject on Unity navmesh:

UnityEngine.AI.NavMeshTriangulation navMeshData = UnityEngine.AI.NavMesh.CalculateTriangulation();
var index = UnityEngine.Random.Range(0, navMeshData.indices.Length - 3);
Vector3 Position = Vector3.Lerp(navMeshData.vertices[navMeshData.indices[index]], navMeshData.vertices[navMeshData.indices[index + 1]], UnityEngine.Random.value);
Vector3.Lerp(Position, navMeshData.vertices[navMeshData.indices[index + 2]], UnityEngine.Random.value);
GameObject MyGameObject = Instantiate(BLABLABLA, new Vector3(Position.x, Position.y, Position.z), Quaternion.Euler(new Vector3(0, UnityEngine.Random.Range(0, 360), 0)));

so, how to do this on A* Recast graph?
and can i use RichAI on Recast graph or have to use AIPath?


#4

Hi

The closest corresponding code would be

// Get all nodes in the graph, make sure to cache this
var nodes = new List<GraphNode>();
AstarPath.active.data.recastGraph.GetNodes(nodes.Add);
// Get 1 point on the graph
var points = PathUtilities.GetPointsOnNodes(nodes, 1);
var rotation = Quaternion.Euler(new Vector3(0, UnityEngine.Random.Range(0, 360), 0));
GameObject MyGameObject = Instantiate(BLABLABLA, points[0], rotation);

This is a uniformly random point on the whole surface. Your approach is biased towards some regions of the navmesh.

The above approach is however not extremely fast, as it has to look through all triangles in the navmesh to be able to sample it uniformly. If you don’t care so much about being super uniform you can use this instead

// Get all nodes in the graph, make sure to cache this
var nodes = new List<GraphNode>();
AstarPath.active.data.recastGraph.GetNodes(nodes.Add);
// Pick a random node
var node = nodes[Random.Range(0, nodes.Count)];
var point = node.RandomPointOnSurface();
var rotation = Quaternion.Euler(new Vector3(0, UnityEngine.Random.Range(0, 360), 0));
GameObject MyGameObject = Instantiate(BLABLABLA, point, rotation);

#5

nice :]

and can i use RichAI on Recast graph or have to use AIPath?


#6

You can use RichAI on recast graphs.
See https://arongranberg.com/astar/docs/movementscripts.html


#7

and does 4.3.x use Jobs / DOTS by default or have to setting up something on 2019.3?

P.S: i just did install Entities package [all of their dependecies installed itself] … and its working like a charm :]


#8

Yes. It uses dots by default.
Of course not everything in the package uses dots, but some systems have been rewritten to make use of it.


#9

hi,

what is equivalent of Unity offmeshlink and OffMeshLinkData and currentOffMeshLinkData on A* Recast / RichAi ?


#10

Take a look at the Nodelink
Use the function onTraverseOffMeshLink on RichAI to detect when you’re traversing the nodeLink.