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
}