0

(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");

}
birajad
  • 482
  • 1
  • 8
  • 16
  • 1
    With the function from the duplicate, you can do `StartCoroutine(moveToX(GameObject.Find(a).transform, GameObject.Find(b).transform, 1.0f));` – Programmer Nov 11 '18 at 16:21
  • Add EDIT to your question and add your new code that's not working then explain what's not working.... – Programmer Nov 11 '18 at 16:42
  • @Programmer (s)he's breaking inside the coroutine if there's another one running, so a moves, but b hits the break and doesn't move. I can post this as the answer if you open the question. – Arshia001 Nov 13 '18 at 04:18
  • @Arshia001 The question was edited to include the code from the duplicate. That wasn't the original code and there was no move code in the original question. The code from the duplicate shows to how to move to another location with x duration. OP can create new question and describe current issue in that question – Programmer Nov 13 '18 at 04:24
  • I didn't realize that. Still, OP's just one if away from the answer XD – Arshia001 Nov 13 '18 at 04:39

0 Answers0