Movement is bouncy when using Vector3.MoveTowards?

I managed to implement a basic AI script to follow a target around.

But for some reason the movement is bouncy. Basically what’s happening is when the path is recalculated, the capsule I’m moving using the code below is going to the first waypoint in the next path, which seems to be the previous waypoint in the current path. This happens with and without the funnelmodifier. The code is below:

Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
        dir *= speed;

transform.position = Vector3.MoveTowards(transform.position, dir+transform.position, speed);

I actually managed to fix it by changing the OnPathComplete to reset the currentWaypoint counter to 1 instead of 0, but I’m wondering if this is a correct solution, and as to why this is happening in the first place?


I’m guessing it happens because path requests take a small amount of time to complete, so your agent has moved a bit and when you reset the waypoint counter to 0 it will try to move back to where it just was when the path request was started.
I suggest moving your code for update the currentWaypoint field to the top of the Update method and also to put it inside a while loop so that it can increment the field multiple times per frame. The script made in the Get Started tutorial is very basic and it lacks a lot of the finer details. You can also use one of the included movement scripts which already include all this.

Hey Aron. Thank you for the quick reply.

Moving the updating code to the top fixed it like you said. Thanks! :slight_smile:

I will check out the other code as well. :smiley:

1 Like