Expected behaviour:
The A.I. should again start to follow target
Actual behaviour:
The A.I. triggers reachedEndOfPath in PathReady callback when it is clearly long way away from the target. The problem only manifests when using FleePath interrupted by seeking path
Version:
AStar Beta Pro 4.3.46
Unity 2021.1.12f1
Screen:
Relevant code:
private void UpdateMode()
{
_prevMode = CurrentMode;
var seeker = GetComponent<Seeker>();
switch (CurrentMode)
{
case Mode.Follow:
Debug.LogWarning("Started following!");
seeker.StartPath(transform.position, Player.position, PathCompleted);
break;
case Mode.Flee:
Debug.LogWarning("Started fleeing!");
var path = FleePath.Construct(transform.position, Player.position, 10000);
path.aimStrength = 1;
path.spread = 4000;
seeker.StartPath(path, PathCompleted);
break;
}
}
private void PathCompleted(Path p)
{
var richAI = GetComponent<RichAI>();
if (richAI.reachedEndOfPath)
{
Debug.LogError("I've already reached the end of path!!!");
richAI.isStopped = true;
}
}
This is very likely because at the point where your PathCompleted function runs, the RichAI hasn’t even been notified that the path was calculated (its callback will likely run right after this one).
When using the built-in movement scripts I’d recommend that you let the movement script itself handle the communication with the seeker. Use something like
case Mode.Follow:
Debug.LogWarning("Started following!");
seeker.StartPath(transform.position, Player.position, PathCompleted);
break;
As I’ve written, the problem only manifest when seeking path (Follow case) on RichAI directly (but a few moments later) after using flee path (Flee case). If I use follows multiple times, it is okay. If I use flee multiple times it is also okay.
See also log from the original post: the Path Completed log reports success in Following with Path of Length 10. Yet still callback called later, but in the same frame has reached end set as true.
Ah, but here you have the exact same issue that I talked about earlier. The Seeker will call your callback before the RichAI’s callback is called. That means the RichAI hasn’t been able to reset the reachedEndOfPath property yet. At least, I think this is the most likely explanation.
I’d recommend that you use ai.SetPath here too. You can check when the path has been calculated using ai.pathPending.
I tried to replicate any issues with the reachedEndOfPath property, but to me it always seems to be correct after the RichAI’s callback has been executed.