How do I swap positions of two objects smoothly?

I am making a game of finding the real bottle and in each turn the bottles swap positions in a triangular manner. They merge instead of swapping places and also the script is framerate based and I tried to do it with while loop and it swaps the positions in one frame.[37591-screenshot+(34).png|37591]

using UnityEngine;
using System.Collections;

public class SwitchPlaces : MonoBehaviour 
{
	public int speed=1;
	public GameObject[] bottles = new GameObject[6];

	// Use this for initialization
	void Start ()
	{
	
	}
	
	// Update is called once per frame
	void Update ()
	{
		Swap (bottles[0],bottles[1],bottles[2]);
	}

	void Swap(GameObject b1, GameObject b2, GameObject b3)
	{
		b1.transform.position = Vector3.MoveTowards(b1.transform.position
		                                     ,b2.transform.position
		                                     ,speed*Time.deltaTime);

		b2.transform.position = Vector3.MoveTowards(b2.transform.position
		                                     ,b3.transform.position
		                                     ,speed*Time.deltaTime);

		b3.transform.position = Vector3.MoveTowards(b3.transform.position
		                                            ,b1.transform.position
		                                            ,speed*Time.deltaTime);
	}
}

Hope You Are Looking For This…`

using UnityEngine;
using System.Collections;

public class Testing : MonoBehaviour {

public Transform test1;
public Transform test2;
Vector3 tempPos1;
Vector3 tempPos2;
// Use this for initialization
void OnEnable () {
	tempPos1 = test1.position;
	tempPos2 = test2.position;
	StartCoroutine (Swap (10));
}

IEnumerator Swap(float time)
{
	float i = 0;
	while (i<1) 
	{
		i+=Time.deltaTime/time;
		test1.position=Vector3.Lerp(test1.position,tempPos2,i);
		test2.position=Vector3.Lerp(test2.position,tempPos1,i);
		yield return 0;
	}
}`

Have a look at this line here:

b1.transform.position = Vector3.MoveTowards(b1.transform.position
                                          ,b2.transform.position
                                          ,speed*Time.deltaTime);

Notices that b1 is moving towards b2.
It is ‘not’ moving towards where b2 started out. This is why they are meeting in the middle.
To prevent this, before you start moving the bottles, you should note their starting positions, and move towards that.

Here is a direct solution to your problem.

using UnityEngine;
using System.Collections;

public class SwitchPlaces : MonoBehaviour
{
    public int speed = 5;
    public GameObject[] bottles = new GameObject[6];
    public Vector3[] startingPositions = new Vector3[6];

    // Use this for initialization
    void Start()
    {
        SetStartingPositions();
    }

    void SetStartingPositions()
    {
        for (int i = 0; i < 6; i++)
        {
            startingPositions _= bottles*.transform.position;*_

}
}

// Update is called once per frame
void Update()
{
Swap(bottles[0], bottles[1], bottles[2]);
}

void Swap(GameObject b1, GameObject b2, GameObject b3)
{
b1.transform.position = Vector3.MoveTowards(b1.transform.position, startingPositions[1], speed * Time.deltaTime);
b2.transform.position = Vector3.MoveTowards(b2.transform.position, startingPositions[2], speed * Time.deltaTime);
b3.transform.position = Vector3.MoveTowards(b3.transform.position, startingPositions[0], speed * Time.deltaTime);
}
}
In Start, we set the starting positions.
In Swap, we replace the current positions of the target bottles with their starting positions.
If you do it in a while loop, the whole loop will execute in a frame.
Coroutines are perfect for when you want to split execution of that loop into multiple frames. They are also great because they don’t get called every frames, only while they’re still not finished.
This was kinda fun so I tool the liberty of extending it using coroutines. :slight_smile:
Below is my extended solution. Let me know if you have any questions :).
using UnityEngine;
using System.Collections;

public class SwitchPlaces : MonoBehaviour
{
public int speed = 10;
public int totalSwaps = 10;
public GameObject[] bottles = new GameObject[6];

private Vector3[] startingPositions = new Vector3[6];
private int totalBottles;

void Start()
{
totalBottles = bottles.Length;
SetStartingPositions();
StartCoroutine(RunSwaps());
}

void SetStartingPositions()
{
for (int i = 0; i < totalBottles; i++)
{
startingPositions = bottles*.transform.position;*
}
}

IEnumerator RunSwaps()
{
int swap = 1;
int firstBottle = 0;

while (swap <= totalSwaps)
{
yield return StartCoroutine(SwapPositions(firstBottle, (firstBottle + 1) % totalBottles, (firstBottle + 2) % totalBottles));
swap++;
firstBottle = (firstBottle + 1) % totalBottles;
SetStartingPositions();
}
}

IEnumerator SwapPositions(int a, int b, int c)
{
while (Vector3.Distance(bottles[a].transform.position, startingPositions**) > 0.01f ||**
Vector3.Distance(bottles**.transform.position, startingPositions**
```c
) > 0.01f ||
Vector3.Distance(bottles[c].transform.position, startingPositions[a]) > 0.01f)
{
bottles[a].transform.position = Vector3.MoveTowards(bottles[a].transform.position, startingPositions
, speed * Time.deltaTime);
bottles**.transform.position = Vector3.MoveTowards(bottles**.transform.position, startingPositions[c], speed * Time.deltaTime);
bottles[c].transform.position = Vector3.MoveTowards(bottles[c].transform.position, startingPositions[a], speed * Time.deltaTime);
yield return null;
}

        bottles[a].transform.position = startingPositions**;
        bottles**.transform.position = startingPositions[c];
        bottles[c].transform.position = startingPositions[a];
    }
}**</strong></em></em></em></em>

```

you can use Dotween DoMove() functions