Move Sprite to position then execute code

Hey guy.

I can’t believe i’m having to ask this, as it seems so simple. However my brain just dosn’t seem to be working currently.

I’m working on a card game (Klondike) and i’ve finished the majority of the gameplay, and now i’m working on the fancy stuff, in this case the “animation” of card movement.

Layout is pretty simple. I have gameobjects with a “CardStack” script which represent foundations, tableau etc. and i have gameobjects with an attached Card script which represent the cards. Now i have a script on the cards that allows double clicking to automatically move them to a valid place.

What happens now is the originating stack has “Pop” called which removes the card from that stack and the target stack gets “Push” called which adds the card to that stack, on adding a card the card’s transform.Parent is set to the new stack.

What i’m wanting to do is instead of the instant movement from one stack to another, I’m wanting to move the card slower between them, and then add the card to the stack when it arrives (Still only taking < second)

I’ve tried a coroutine that lerps the movement while the card position != target’s position and the outside the while called the correct “Push” but that executed instantly still.

I’m sure this is pretty simple. but how would i go about achieving this?

Can you post your coroutine?

Sure:

    public IEnumerator PushAnimate(GameObject card)
    {
        while (card.transform.position != transform.position)
        {
            Vector3 targetPos = transform.position; ;
            transform.position = Vector3.Lerp(card.transform.position, targetPos, 0.00001f * Time.deltaTime);
            yield return null;
        }
        //We have moved correctly?
        Card c = card.GetComponent<Card>();
        Push(c);
    }