Support Forum

[Bug] RichAI has 'reachedEndOfPath' set as true in PathCallback after using FleePath

See attached mini repro scene (Note: only Assets folder is included, start a new project and copy over assets as well as link to Astar in manifests):

https://drive.google.com/file/d/1w86WFBJmhYjxXM07cdW8YLqkmkUnuBtK/view?usp=sharing

Steps to reproduce:

  1. Press “Q” to follow target
  2. Press “E” to flee from a target
  3. Wait a bit when fleeing
  4. Press “Q” again to follow target

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:
obraz

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;
        }
    }

Hi

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

var path = FleePath.Construct(transform.position, Player.position, 10000);
path.aimStrength = 1;
path.spread = 4000;
GetComponent<RichAI>().SetPath(path);

instead.

See also Documentation

Hello Aron,

I just replaced the case for flee with:

case Mode.Flee:
    Debug.LogWarning("Started fleeing!");
    var path = FleePath.Construct(transform.position, Player.position, 10000);
    path.aimStrength = 1;
    path.spread = 4000;
    var richAI = GetComponent<RichAI>(); // Line changed
    richAI.SetPath(path); // Line changed
    break;

And run the repro steps again. The result was the same and the problem still exists

Hi

I’m not quite sure how your repro looks like given that your PathCompleted method is no longer called?

Hello Aron,

It is called by the code of above case:

            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.

Hi

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.