- A* version: 5.2.5
- Unity version: 2022.3.42f1
I followed the getting started guide setup with a plane, capsule, and grid graph setup: Get Started Guide - A* Pathfinding Project
I then wanted to implement some random “wander” logic for my AI.
On Start(), I use Wandering AI Tutorial - A* Pathfinding Project to select and navigate to a random location:
private void Start()
{
_aiPath = GetComponent<AIPath>();
if (_aiPath == null)
{
Debug.LogError("AIPath component not found! Attach this script to a GameObject with AIPath.");
return;
}
// Start initial wandering behavior
GotoRandomDestination();
}
On Update, I am checking whether the AI reached the location with:
private void Update()
{
Debug.Log($"Distance to destination: {Vector3.Distance(transform.position, _aiPath.destination)}, reachedDistance: {_aiPath.reachedDestination}");
// Do nothing if we didn't reach destination
if (!_aiPath.reachedDestination) return;
// Decrease the timer if it's greater than 0
if (_timer > 0f)
{
_timer -= Time.deltaTime;
}
else
{
// Timer has expired, pick a new destination
_timer = wanderDelay; // Reset the timer
GotoRandomDestination();
}
}
GotoRandomDestination is simple:
private void GotoRandomDestination()
{
// Pick a random walkable point on the graph
var sample = AstarPath.active.graphs[0].RandomPointOnSurface(NNConstraint.Walkable);
// Use the random point as the destination
_aiPath.destination = (Vector3)sample.position;
Debug.Log($"New wandering destination: {_aiPath.destination}");
}
But reachedDestination is always false. I logged the destination point and it’s always 1.00 whilst the AI only manages to get within 1.08 of the distance on the Y axis. I assume this is the problem, but not sure how to fix it.
I know there is a way to override what is considered “reached” threshold with endReachedDistance, but I would prefer not to do so. I would like to fix the root cause of why my AI can never reach its true path. I believe it should be possible because it’s not a multi-level building, just a simple flat plane.
I noticed the “skin width” property on the AI character controller is 0.08, and after changing that to the minimum of 0.00001 (or so), my AI can get closer - so I assume it’s related to this. But a value of 0 is not valid here.
I don’t know why that is causing it or what skin depth even is, but what might a solution be to let the AI navigate fully to the end of the path? My AI is perfectly positioned with a Y axis of 1 in the editor, but as soon as I hit play mode it goes to 1.08.