A* Pathfinding Project

Issue StartMultiTargetPath vs StartPath


#1

Hi,

I have a issue when using StartMultiTargetPath that does not appear when I’m using the normal StartPath. I made a tasksystem, and a unit first checks with Astar which task is closest to him.

Some tasks have a single position, for them I use “seeker.StartPath” and that works perfectly. Some have multiple positions, for them I use “seeker.StartMultiTargetPath”.

foreach (var taskValue in taskDictionary.Values.ToList())
{
    if (taskValue.AccesPositionList.Count > 0)
    {
        var path = seeker.StartMultiTargetPath(transform.position, taskValue.AccesPositionList.ToArray(), false);
        yield return StartCoroutine(path.WaitForPath());

        if (path.error != true)
        {
            float taskDistance = path.GetTotalLength();
            if (taskDistance < dist)
            {
                dist = taskDistance;
                task = taskValue;
                pos = path.originalEndPoint;
            }
        }
    }
    else
    {
        var path = seeker.StartPath(taskValue.TaskPosition, transform.position);
        yield return StartCoroutine(path.WaitForPath());

        if (path.error != true)
        {
            float taskDistance = path.GetTotalLength();
            if (taskDistance < dist)
            {
                dist = taskDistance;
                task = taskValue;
                pos = taskValue.TaskPosition;
            }
        }
    }
}

While “seeker.StartMultiTargetPath” does work, the downside is that the unit immediately starts moving, it does not do this with “seeker.StartPath”

Is there a reason for this? I only want the path length at this point, and not yet to move the unit.

On this script I only use

gameObject.GetComponent<Seeker>();

On another script, that handles the movement I have

gameObject.GetComponent<AIPath>(); 

Both are on the same GameObject though.

Thanks in advance!


#2

Hi

If you are using on the of the built-in movement scripts it should start moving with the other one as well. Not sure why it doesn’t for you.
Generally the Seeker is written to handle one path at a time.
If you want to calculate other paths it is often better to use the AstarPath class directly. So you can use AstarPath.active.StartPath(path).

See this page for more info: https://arongranberg.com/astar/docs/callingpathfinding.html
Then when you have a path you want the agent to follow you could either set ai.destination or you could set the path directly using ai.SetPath.