Recalculating path makes agent turn opposite direction

Hi there!

I’m new to this project and I’m having some trouble with my AI move behaviour that I hope someone here can help with.
I’m running into an issue where every time I recalculate the agents path to its targetDestination, in a single frame the direction is flipped.
I’m guessing it has something to do with when a new path is calculated, the agent is already ahead of the starting node in the new path, meaning it turns around for at split second or something?

    protected override void OnUpdate()
    {
        if (recalculatePathTick.value > 0.0f)
        {
            internalTimer -= Time.deltaTime;

            if (internalTimer <= 0.0f)
            {
                internalTimer = recalculatePathTick.value;
                CalculatePath();
            }
        }

        if (currentPath != null && currentPath.CompleteState == PathCompleteState.Complete)
        {
            if (currentWaypoint >= currentPath.vectorPath.Count)
            {
                EndAction(true);
                return;
            }   

            currentDirection = (currentPath.vectorPath[currentWaypoint] - enemy.transform.position).normalized;
            currentDistance = Vector2.Distance(enemy.transform.position, currentPath.vectorPath[currentWaypoint]);

            if (currentDistance < nextWaypointDistance.value)
            {
                currentWaypoint++;
            }
        }

        previousPosition = enemy.transform.position;

        enemy.Input.SetAxisDouble(InputAction.LeftStick, currentDirection);
    }

    private void CalculatePath()
    {
        pathSeeker.StartPath(enemy.transform.position, To.value, OnPathCalculated);
    }

    private void OnPathCalculated(Path newPath)
    {
        ABPath p = newPath as ABPath;

        if (p == null || p.error)
        {
            return;
        }

        currentPath?.Release(this);

        currentPath = p;
        currentPath.Claim(this);

        currentWaypoint = 0;
        //justCalculated = true;
    }

I’m also using this together with NodeCanvas, which is why I have EndAction() functions etc.

Is there any way to smooth out the positions, whenever the path is recalculated somehow?

Thanks!

Hi

This usually happens because path requests are asynchronous. So when the path has been calculated the start of the path is behind the agent and it will then turn around for a single frame.

The built-in scripts (AIPath/RichAI/AILerp) have some code to help mitigate this issue. Using those should be much easier than using a custom script to begin with.

Wow, thanks for the quick reply. Will take a look at those scripts!

Also, right after I posted, I tried setting the currentWaypoint to 1 instead of 0 when a new path is recalculated, which seemed to fix it. But I’m pretty sure it’s not a good solution in the long run?

Indeed. What you could instead do is to immediately when a path has been calculated check recursively if the current waypoint is close enough that you can increment the currentWaypoint index.

1 Like

Genius, that worked perfectly! Will stick with this for now, but I’m gonna give those example scripts a look as well.
Again, thank you!

1 Like