If I had to guess, the reason the position is slightly off is because the length of the distance the object is from the target isn't taken into account, so sometimes it overshoots it a bit and goes too far.
I think something like this might work:
public override void _Ready()
{
bodyAnimation = GetNode<AnimatedSprite>("Body");
legsAnimation = GetNode<AnimatedSprite>("Legs");
totalMovePoints = GetNode("MovePoints").GetChildCount();
movePoints = GetNode("MovePoints");
target = movePoints.GetChild<Position2D>(currentTargetNumber).GlobalPosition;
}
public override void _PhysicsProcess(float delta)
{
if(!shouldMove)
{
shouldMove = true;
if(currentTargetNumber == totalMovePoints - 1)
{
currentTargetNumber = 0;
}
else
{
currentTargetNumber += 1;
}
target = movePoints.GetChild<Position2D>(currentTargetNumber).GlobalPosition;
}
if((target - GlobalPosition).Length() < 2f)
{
GlobalPosition = target;
shouldMove = false;
}
if(shouldMove)
{
Vector2 positionToTarget = (target - GlobalPosition)
if (movementSpeed * delta >= positionToTarget * delta)
{
MoveAndSlide((target - GlobalPosition).Normalized() * movementSpeed);
}
else
{
MoveAndSlide(positionToTarget)
}
}
}
I have not tested the code, and I have not done much C# with Godot, but I think that should fix the issue. Now when movement_speed is more than the length of the distance to the target, it will instead use the the distance to the target so that it does not go too far.
(We multiply by delta in the comparisons because MoveAndSlide automatically multiplies by delta, I think...)
I do not know if the code above will work for sure, but that is what I would try. :smile: