(I am posting this question because none of the answers provided before worked out for me. Sorry if it sounds like the same question as before.)
I need to switch two cards on click of a button. Although the cards switch with the code I have written, they get swapped so fast. but I need to make them look like moving to each other's location. This is what I have done so far. Now I could move a to b but b is not changing it's position at all.
IEnumerator moveToX(Transform fromPosition, Vector3 toPosition, float duration)
{
//Make sure there is only one instance of this function running
if (isMoving)
{
yield break; ///exit if this is still running
}
isMoving = true;
float counter = 0;
//Get the current position of the object to be moved
Vector3 startPos = fromPosition.position;
while (counter < duration)
{
counter += Time.deltaTime;
fromPosition.position = Vector3.Lerp(startPos, toPosition, counter / duration);
yield return null;
}
isMoving = false;
}
void swap(string a, string b)
{
//float step = speed * Time.deltaTime;
Vector3 temp = GameObject.Find(a).transform.position;
StartCoroutine(moveToX(GameObject.Find(a).transform, GameObject.Find(b).transform.position, 1.0f));
//Debug.Log(GameObject.Find(b).transform.position + " => " + temp);
StartCoroutine(moveToX(GameObject.Find(b).transform, temp, 1.0f));
}
//Button activation(just for testing if swap method worked)
public void ButtonClicked()
{
swap("Two", "Three");
}