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.
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>
```