Following path smoothly

I’m using A* algorithm to find a path which is put into an array of vectors which each point of the path is at. I then have the objects following it going to each point until its reaches the last. But the objects are jittering when they move because when they move they go a bit past the alignment of next point then go back and fourth trying to align. How can i fix this? I have tried adding a variable to make them require a bit more movement to align but when i do that sometimes they get stuck.

note: All the path positions are adjacent to each other. Also the movement has to be very accurate otherwise the object will get stuck on corners.

Vector2 velocity = new Vector2(0,0);
            float offsetSmooth = 0f;
            //path is the array of vectors
            if (Vector2.Distance (path [pathSequence], transform.position) <= 0.1f) {
                pathSequence--;
            }

            if (pathSequence <= 1) {
                pathSequence = 0;
                pathFound = false;
            }

            if (path [pathSequence].x > transform.position.x + offsetSmooth) {
                velocity.x = 1;
            } else if (path [pathSequence].x < transform.position.x - offsetSmooth) {
                velocity.x = -1;
            }

            if (path [pathSequence].y > transform.position.y + offsetSmooth) {
                velocity.y = 1;
            } else if (path [pathSequence].y < transform.position.y - offsetSmooth) {
                velocity.y = -1;
            }

            UMovement.MoveVel (velocity.x, velocity.y, 300 );  // x velocity, y velocity, speed
    }
1 Like

You said that you added a variable to try to fix it, which part of your code is that? The first if block, or is the variable offsetSmooth?

If I’m understanding you correctly, you’re looking for something like this:

if (Vector2.Distance(path[pathSequence], transform.position) <= closeEnoughDistance) {
  transform.position = path[pathSequence]; // snap on to the waypoint
  pathSequence--;
}

I don’t want the object to snap to the next position but move towards it. My only problem is getting it very accurate and smooth and yes the offsetSmooth is the variable i added to try to get it to stop shaking but that caused more problems like it getting stuck at some positions since it wasn’t accurate. What the code above does is checks if its close enough to its target position then sets the new target position, pathSequence is the variable which defines which position in the array to go for.

What you’re describing is a steering behavior. The typical approach is to Seek to each point and then Arrive at the last one. Check out the link I posted.