- Home /
Animate fanning cards almost works!
I have a list of cards set with a starting and ending position. When I simply place the cards, they are placed in the scene as a fanned-out deck. Like so:

I want to animate this so that the deck of cards moves along the angle and the cards drop into place to create the final look. For the most part, I have a function that does this, but I am having some conflicts with proper placement. Here the designed function.
I would also like to know the best way to turn the cards while they move along the arc. Thanks!
IEnumerator AnimateCardSpread()
{
Vector3 newPos=memoryCards[0].startPos;
int k=0;
while(k<memoryCards.Count)//memoryCards is the List of card objects with established start/end positions
{
foreach(CardController c in memoryCards)
{
//has the card reached it's endPos?
//I THINK THIS IS WHERE THE PROBLEM LIES
if(c.endPos == newPos)
{
c.transform.position=newPos;
}
}
// calculate current time within our lerping time range
float cTime = Time.time * 10f;
// calculate straight-line lerp position:
Vector3 currentPos = Vector3.Lerp(memoryCards[k].startPos, memoryCards[k].endPos,cTime);
//Vector3 currentPos = Vector3.Lerp(new Vector3(-4,0,0), new Vector3(4,0,0), cTime);
// add a value to Y, using Sine to give a curved trajectory in the Y direction
currentPos.y += animSpeed * Mathf.Sin(Mathf.Clamp01(cTime) * Mathf.PI);
// finally assign the computed position to our gameObject:
newPos = currentPos;
k++;
yield return new WaitForSeconds(.02f);
}
}
So, I was just overcomplicating things. Here is my solution:
IEnumerator AnimateCardSpread()
{
int k=0;
for(float i=0;i<1f;i+=animSpeed)
{
float cTime = Time.time * 10f;
Vector3 currentPos = Vector3.Lerp(memoryCards[k].startPos, memoryCards[k].endPos,cTime);
currentPos.y += animSpeed * $$anonymous$$athf.Sin($$anonymous$$athf.Clamp01(cTime) * $$anonymous$$athf.PI);
memoryCards[k].transform.position=currentPos;
if(k>=memoryCards.Count-1)
k=0;
else
k++;
yield return new WaitForSeconds(animSpeed);
}
}
Answer by Whelandrew · May 19, 2015 at 10:29 PM
So, I was just overcomplicating things. Here is my solution:
IEnumerator AnimateCardSpread()
{
int k=0;
for(float i=0;i<1f;i+=animSpeed)
{
float cTime = Time.time * 10f;
Vector3 currentPos = Vector3.Lerp(memoryCards[k].startPos, memoryCards[k].endPos,cTime);
currentPos.y += animSpeed * Mathf.Sin(Mathf.Clamp01(cTime) * Mathf.PI);
memoryCards[k].transform.position=currentPos;
if(k>=memoryCards.Count-1)
k=0;
else
k++;
yield return new WaitForSeconds(animSpeed);
}
}
$$anonymous$$ay you explain where ti add this piece of code? thanks :)
Your answer