ConstantPath path = ConstantPath.Construct (transform.position, randomSearchingLength);
AstarPath.StartPath(path);
path.BlockUntilCalculated();
var singleRandomPoint = PathUtilities.GetPointsOnNodes(path.allNodes, 1)[0];
I use the above code to find a random location.
(I followed the tutorial.)
However, it only searches narrow places.
I want to set the distance to explore.
What should I do?
What exactly do you need the random point for?
I use this code to plant mines around the unit at radom places:
Vector3 originalPos = unit.transform.position;
//pick random position around unit
Vector3 rand = UnityEngine.Random.onUnitSphere * 10;//random inside 10m rad
Vector3 targetPos = new Vector3(originalPos.x + rand.x, originalPos.y, originalPos.z + rand.y);
//validate target pos: on graph and reachable
if (((RecastGraph)AstarPath.active.graphs[unit.GetNavGraphIndex()]).PointOnNavmesh(targetPos, null) != null)
{
NNConstraint pathConstraint = Utilities.GetPathConstraint(unit);
GraphNode node1 = AstarPath.active.GetNearest(unit.transform.position, pathConstraint).node;
GraphNode node2 = AstarPath.active.GetNearest(targetPos, pathConstraint).node;
if (node1 != null && node2 != null && PathUtilities.IsPathPossible(node1, node2))
{
//Valid position, do stuff
}
}
1 Like