Here is my current code. Not sure how to fix with without a while loop of recursive solution, but which case more issues. How do I make it keep trying or why is it unable to find nodes in the first place?
using UnityEngine;
using System.Collections;
using Pathfinding;
using System.Collections.Generic;
public class WanderingDestinationSetter : MonoBehaviour
{
public float radius = 20;
IAstarAI ai;
void Start()
{
ai = GetComponent<IAstarAI>();
}
private void OnDrawGizmos()
{
Gizmos.DrawSphere(this.gameObject.transform.position, radius);
}
Vector3 PickRandomPoint()
{
var startNode = AstarPath.active.GetNearest(transform.position).node;
var nodes = PathUtilities.BFS(startNode, 20);
var singleRandomPoint = PathUtilities.GetPointsOnNodes(nodes, 1)[0];
var multipleRandomPoints = PathUtilities.GetPointsOnNodes(nodes, 100);
return singleRandomPoint;
//var point = Random.insideUnitSphere * radius;
//point.y = 0;
//point += ai.position;
//return point;
}
void Update()
{
// Update the destination of the AI if
// the AI is not already calculating a path and
// the ai has reached the end of the path or it has no path at all
if (!ai.pathPending && (ai.reachedEndOfPath || !ai.hasPath))
{
ai.destination = PickRandomPoint();
ai.SearchPath();
}
}
}