Pathfinding agent can't reach end-of-path state

I’ve built basic pathfinding in my code with this Brackeys tutorial, and it seems Brackeys made a mistake somewhere because my AI is always in create-path/follow-target states, it never stops. People on youtube say that the problem is in incrementing currentWaypoint, but I can’t figure out why. Here’s the code itself.

public Transform target; 

public float speed = 200f;
public float nextWaypointDistance = 3f; 

Path path;
int currentWaypoint;

Seeker seeker;
Rigidbody2D rb;

void Start()
{
    seeker = GetComponent<Seeker>();
    rb = GetComponent<Rigidbody2D>();
    animator = GetComponent<Animator>();

    InvokeRepeating("UpdatePath", 0f, 1f);
}

void UpdatePath()
{
    if(seeker.IsDone())
        seeker.StartPath(rb.position, target.position, OnPathComplete);
}

void OnPathComplete(Path p)
{
    if (!p.error)
    {
        path = p;
        currentWaypoint = 0;
        
    }
}

void FixedUpdate()
{
    if (path == null)
        return;

    if (currentWaypoint >= path.vectorPath.Count)
    {
        reachedEndOfDestination = true;
        return;
    }
    else
    {
        reachedEndOfDestination = false;
    }

    Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
    Vector2 force = direction * speed * Time.deltaTime;

    rb.AddForce(force);

    float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);

    if (distance < nextWaypointDistance)
        currentWaypoint++;
    }
if (currentWaypoint >= path.vectorPath.Count) {...}

should be

if (currentWaypoint + 1 >= path.vectorPath.Count) {...}

since the last waypoint will be at the position path.vectorPath.Count-1 because the count is the number of points and the path list starts counting at 0

1 Like

I’m afraid that’s not it. +1 only makes it worse, agent moves slower and jittery.
What I want to do is to tell the agent that it has to stop if the target is reached and that’s it. Right now that doesn’t happen. It reaches it, but continues to walk in place.

oops, you’re right, I didn’t pay attention to the fact that it increments at the end of the loop, not the start, your original code was correct as it would reach the end and then increment, making it equal.

it probably isn’t stopping because you don’t actually do anything with the reachedEndOfDestination variable
sticking the direction, force, and AddForce lines inside of if (!reachedEndOfDestination) should do it

    if (!reachedEndOfDestination) {
        Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
        Vector2 force = direction * speed * Time.deltaTime;
        rb.AddForce(force);
    }
1 Like

also it would be a good idea to add
reachedEndOfDestination = false;
into OnPathComplete so that it doesn’t reach the end once and stop forever

void OnPathComplete(Path p)
{
    if (!p.error)
    {
        path = p;
        currentWaypoint = 0;
        reachedEndOfDestination = false;
    }
}
1 Like

Well first of all a huge thank you for your time on this!
I’ve implemented everything but still it doesn’t seem to be working. I even added
else {force = new(0,0); to your suggested if but it doesn’t stop completely.
When reachedEndDestination is true agent still moves towards the target but really slowly. Maybe because nextWaypointDistance is actually used to check the end of the path?

since you are using AddForce there will be some velocity left over at the end, this might be the problem, try this for the reached end part

    if (currentWaypoint >= path.vectorPath.Count)
    {
        reachedEndOfDestination = true;
        rb.velocity = Vector2.zero;
        return;
    }

note that zero might have to be uppercase

1 Like

That did work, thank you! But weird. Agent stops now, but force is still a non-zero value. Because of that when agent stops he can’t go into idle animation state. I’ve already tried assigning force = Vector2.zero in if (reachedEndDistance = true), but that didn’t do anything.
Is there something else I miss?

I don’t know as I haven’t used forces and instead edited velocity directly and based my animations off of velocity in almost all of my projects thus far

1 Like

Thank you A LOT for your help! I think I’ll be able to take it from here :slight_smile: