# Card flipping over time

I’m trying to have a gameobject appear on screen rotate around the Z axis a number of times, start to slow down and then stop facing the camera (original Z 0 or Z 180, whichever is easier). (A playing card that changes it’s value each time it flips over, then stops to show the final card)
something along these lines:

``````    public int flipTimes = 8;
public float RotationSpeed = 150;
public Material[] cardFaces;
public int finalValue;

// Update is called once per frame
void Update()
{
if(Mathf.Abs(transform.eulerAngles.z) > 175){flipTimes--; transform.Rotate(Vector3.forward *0);}        //was one flip made, set rotation to zero
// insert code to change material so face card changes using cardFaces array
// randomCard = Random.Range(1, cardFaces.Length);
// transform.GetComponent<MeshRenderer>().material = cardFaces[randomCard];
if(flipTimes < 4){RotationSpeed -= RotationSpeed * Time.deltaTime;}     //if the number of flips is getting to the end, slow down the card
if(flipTimes < 1 && transform.eulerAngles.z < 5)                        //if the flip count is over and the card is facing the camera, stop spinning and record final value
{
RotationSpeed = 0;
finalValue = randomCard;
}
else
(
transform.Rotate ( Vector3.forward * ( RotationSpeed * Time.deltaTime ) );      //keep rotating at speed if required
)
}
``````

Any help getting this to run (smoothly) would be much appreciated. I’ve been working on it for a while, but I am quite rusty and don’t have a ton of experience.

thanks

I got something working so I thought I’d put what I have working here in case anyone else wants it.

``````    public bool rotating;
private int flipCount = 4;

private void Update()
{
if(!rotating)
{
if(flipCount > 0)
{
rotating=true;
StartCoroutine(RotatePlatform(180));
flipCount--;
}
}
}

private IEnumerator RotatePlatform(float dir)
{
Vector3 target = new Vector3(transform.eulerAngles.x,transform.eulerAngles.y, (transform.eulerAngles.z + dir + 360) % 360);
while(Mathf.Abs(transform.eulerAngles.z - target.z) >= Mathf.Abs(dir * Time.deltaTime))
{
transform.Rotate(new Vector3(0,0,dir) * Time.deltaTime);
yield return null;
}
transform.eulerAngles = target;
rotating = false;
}
``````

I’ll still have to add the material code, but that shouldn’t be too bad.