Vector2.MoveTowards with many objects having inconsistant speeds

Hi all,

I am looking to solve an issue with Vector2.MoveTowards producing spacing between the containers in the images below. The first image showcases how the containers are initially set up and positioned on the first flight to the planet. The second image shows the return journey, after changing the target for the moveTowards back to the starting position. As you can see the containers are very misaligned. The faster the ship goes, the worse this gets.

Sadly I cannot parent due to the fact that the containers overlap the ship and all collect seperatly from the planets. Almost looks like a snake effect in gameplay.


Here is the important parts of the code that I am using for the movement.

if (transform.position != currentTarget)
                {
                    transform.position = Vector2.MoveTowards(transform.position, currentTarget, speed * Time.deltaTime);
                }
                else
                {
                    GetNextTarget();
                }
void GetNextTarget()
        {
            currentTarget = ship.CurrentTarget;
            transform.up = currentTarget - transform.position;
        }

This is how I initially set up the containers

public void SetCarriageInitialPosAndRot()
        {
            Vector3 carriagePosition = ship.transform.position;
            Vector3 carriageDirection = ship.Visual.transform.up;
            Vector3 newPos = carriagePosition + -carriageDirection * CarriageGap;
            transform.position = newPos;
            currentTarget = ship.CurrentTarget;
            transform.up = currentTarget - transform.position;
        }

I have tried a number of ‘fixes’ or workarounds including manually placing the carriages back to their ‘correct’ positions and introducing a variable speed depending on distance from the container in front.

Both helped a little, but still the containers had variable gaps.

The misaligned seems to be coming from when the containers change their current target, and potentially the code taking 1 frame longer to complete the change and therefor lagging behind or infront.

Any help or insight would be massively appreciated!

Not sure your circumstances, but possibly try putting the follow code into a LateUpdate(), again I’m not the greatest coder, but it’s helped fix similar issues for me.